Click here to Skip to main content
16,020,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Noob question. But could not find anything.

I have a variable say
C++
double a= 123.07

when I use a
C++
double b = a;

it assign value of a as 123.1 instead of 123.07

How can I remove this rounding off while assigning a double value to another double variable.


About this value what i am doing is , parsing a file into tokens, one of these tokens being the value of variable a.

after breaking the value of token is assigned to a string and then to a float using


double a= atof(amount.c_str());

Now, i am sending this value to a data structure Class created by me.

In the class constructor i initialize a double type class variable by this value.

Class definition :

sortTran(string trantype,string ts,string buf, string amount, double bal, long ts2)

Now when i call class member using object->varname which is nothing but the very same value of double,

i get the value rounded off.

i tried to create a local variable in the calling function and even that is rounding off.

I am stumped !! i could not find anything as my project's output file's binary code is matched with desired output's binary. and even a space bar can screw up the complete thing for me.
Posted
Updated 27-Sep-11 22:20pm
v3
Comments
Chuck O'Toole 28-Sep-11 3:19am    
How do you know the values? What tool or debugger or print statement did you use to display the value to you? Floating point numbers are just a string of bits, in this case 64 of them stored in memory. The assignment statement "b = a" is just going to copy the 64 bits without going through conversions. So the issue must be with the "display" of the number, not the number itself.
typedefcoder 28-Sep-11 4:21am    
i am using cout<<a<<b<<endl;
even that shows 123.07 123.1
[no name] 28-Sep-11 4:54am    
Are you sure you want to use double values here given their inherent inaccuracy. If you are using these for financial calculations then your answers will not be correct in many cases. Floating point (and double) numbers allow very wide precision but at the cost of accuracy, and should be avoided in most cases.
typedefcoder 28-Sep-11 4:59am    
Richard, is it possible for you to check my code ? can i mail you ? i can not post the complete code and was not able to remove this bug. If it is .. please let me know/
[no name] 28-Sep-11 5:24am    
Sorry no, it would be too expensive for you.

There are two mistakes in one, plus one in the preface.

preface


If you want good answer post good question, and explain "how did you know the actual values of a and b". Otherwise all our work is about psychology reverse engineering, not programming.

First conceptual error


Computer normally use binary aritmetic, not decimal. 123.07 is a "constant literal" the compiler let you type for your convenience, but, internally, it is converted on compilation to the nearest binary floating-point representation the better fits a double variable. Exact decimal numbers are not necessarily exact binary number, for the very simple reason that 10 is not a power of 2. In your case, what is stored is probably something like 0,961484375 * 2<sup>7</sup>, where 0,961484375 must be rounded to the nearest binary representation.
So don't assume a float or double variable store exactly the value you write in decimal: a small round happens just at that point.

Second conceptual error


Exact binary floating point values don't necessarily convert into exact decimal numbers for the simple reason that 2 is not a log2(10). After a decimal-to-binary and binary-to-decimal your 123.07 will probably looks like
123.0699999999999999999
But when you print it, the output function you use (that you didn't tell us about) have to let this thing to fit a given length and precision. If these "parameters" for whatever reason you didn't make us aware of, are set to "4 meaningful digits", you got 123.1.
But this is the same for both a and b (just try to print them both ...)
 
Share this answer
 
Comments
[no name] 28-Sep-11 4:52am    
This is the best explanation I have seen of this issue, for a long time, and gets my 5, but deserves more.
OriginalGriff 28-Sep-11 8:23am    
Gets a solid 5 from me as well - good explanation!
How did you check it? If there is a roundoff, it must be the same.
On my system, the code
C++
#include<iostream>
using namespace std; 
int main()
{
  double a= 123.07;
  double b = a;

  cout << "a = " << a << ", b = " << b << endl;
}

gives the following output:
C++
a = 123.07, b = 123.07


while, using the debugger I can see both a, b having value 123.06999999999999.
 
Share this answer
 
My compiler gives 123.07, not 123.1. Which compiler did you used?
C++
#include<stdio.h>

int main()
{    
double a= 123.07;
double b = a;
printf("%lf\n",b);
    
return 0;    
}
</stdio.h>

It comes 123.07
 
Share this answer
 
v2
It doesn't. I know, I know, it looks like it does, but trust me, it doesn't.

What is happening is that the number is being rounded up when you display it. Try putting a breakpoint on teh first instruction of the two and single step through, looking at the variable values as you go. You will find that the assignment doesn't work either! It assigns a value of "123.06999999999999999999" instead... :laugh:
 
Share this answer
 
v2
Comments
Emilio Garavaglia 28-Sep-11 8:01am    
Added a missing 0 ;-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900