Re: Rounding of the double
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Thu, 31 May 2007 16:25:20 -0400
See my example. Indeed, you get 80. That's 80%. What's wrong with that?
If you need to PRINT it as a decimal number, see my code example:
s.Format(_T(%d.%02d), value / 100, value % 100);
Note that this works for any positive value. You need abs(value % 100) if negative
numbers are possible.
But if you ever convert it to a double, you will get some value that is merely a good
approximation to 0.80.
joe
On 31 May 2007 10:55:46 -0700, Alex <alsim123@xxxxxxxxxxx> wrote:
Thanks, Tom,Joseph M. Newcomer [MVP]
It worked, but only half of the way :)
I've got 80.00000000000000000
But I need 0.80000000000000000
And of course I cannot get it, only
0.80000000000000004
And indeed, you are right, I have to calculate some percentage.
So instead of fractions (and correspondingly "double"s), I'll have to
change some code and work with percents (and correspondingly "int"s).
Funny, I've never thought that it's impossible to remove this garbage
from double.
Thanks,
Alex
On May 31, 12:37 pm, "Tom Serface" <tom.nos...@xxxxxxxxxxxxx> wrote:
Hi Alex,
Depending on the size of the numbers you may want to try converting them to
DWORDs and doing the division that way, then convert them back. For
example,
DWORD dw1 = (DWORD) f1 * 100;
DWORD dw2 = (DWORD) f2;
DWORD dw3 = dw1/dw2;
This works well when trying to figure out a percentage calculation for
exmaple.
double fResult = (double) dw3;
Tom
"Alex" <alsim...@xxxxxxxxxxx> wrote in message
news:1180626964.583331.163830@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I'd tried to use already Tom's link, I found it in some previous
posts. It doesn't work.
I'm calling RoundDouble( 0.80000000000000004, 4 )
At the end this function divides 80 by 100 and return of course
0.80000000000000004
So I think the problem is that "double" is also "float" in C++
It may sound stupid, but I'm going to use SQL Server to get the proper
result, it has type "numeric", if in SQL Server I divide numeric =
80.00, by numeric = 100.00 it returns correct result
0.80000000000000000
but if in SQL Server I divide float = 80.00, by float = 100.00 it
returns again result
0.80000000000000004
So SQL Server has numeric type and C++ doesn't. So I'll write some
SQL Server stored procedure, which will be using numeric data types.
Thanks,
Alex
On May 31, 11:36 am, MrAsm <m...@xxxxxxx> wrote:
On 31 May 2007 08:10:16 -0700, Alex <alsim...@xxxxxxxxxxx> wrote:
Everybody,
I'm lost
I have double d1 = 80.00
double d2 = 100.00
double result = d1/d2;
result = 0.80000000000000004 !!??
Why appears this "4" at the end!!??
It really makes difference for me.
Hi,
Floating point math is not an "exact" math (like e.g. integer math) in
computers.
When you do operations on floats (or doubles), you cannot expect an
"exact" result; there could be cancellation, rounding errors, etc.
In addition to Tom's link, you might consider also this:
http://cch.loria.fr/documentation/IEEE754/ACM/goldberg.pdf
MrAsm- Hide quoted text -
- Show quoted text -
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- References:
- Rounding of the double
- From: Alex
- Re: Rounding of the double
- From: MrAsm
- Re: Rounding of the double
- From: Alex
- Re: Rounding of the double
- From: Tom Serface
- Re: Rounding of the double
- From: Alex
- Rounding of the double
- Prev by Date: Re: Check a radio button
- Next by Date: Re: test if a string is a valid 'number'?
- Previous by thread: Re: Rounding of the double
- Next by thread: Re: Rounding of the double
- Index(es):