# Thread: Assigning a Double to a long

1. ## Assigning a Double to a long

Hi I am having a problem on a rounding function.
When I assign a double to a long sometimes it changes the number

so for example

double d = 23.00
long l;

l = d;

and then when I print l it is 22.

Any ideas? I can post more of the code, I mean that the double number isn't just initialised and assigned and maybe what happens to it before affects what is happening?

Thanks !

2. What does your code look like? It prints out fine for me:

Code:
```double x = 23.00;
long y = x;
printf("%ld\n", y);```

3. It's possible that, since 23.0 may not be representable as a double (I think it should be, but who knows...) it stores 22.999999 or something like that. Conversion to integral always means "round down", so 22.99999 gets rounded to 22.

4. 22.99999999 is very possible.

You can do normal rounding with "y = x + 0.5" of course.

gg

5. Thanks, the full code is actually a rounding function and this is it:
Code:
```

double roundd(nm_valor)
double nm_valor;
{
double di_exponente= (double)0.00;
double aux=0.00;
unsigned long aux1 = 0; /* unsigned long aux1 = 0; */
int signo = 1;

if (nm_valor < 0.00)
{
signo = -1;
nm_valor = -nm_valor;
}

/* di_exponente = pow (10, (double) NM_DECIMALES);*/

di_exponente = pow ((double)10, (double) NM_DECIMALES);

aux = ((nm_valor * di_exponente) + 0.5);

aux1 = aux/1;
aux = aux1 / di_exponente;
aux = aux * signo;

return(aux);
}```
And if the value that went in was x.5 the rounded value sometimes was x and sometimes x+1.

Seems part of the problem is with the prints which also do their own rounding...

6. Code:
```double roundd(nm_valor)
double nm_valor;
{```
Could it be that your syntax is just a little outdated?

7. That is very possible but it is all I have to work with, they are a little stupid here in that sense *sigh*

surely out of date syntax doesn't result in bad behaviour?

although it does give problems...but this isn't one of those cases I don't think..

8. So, just so I can see what it going on in different places (looks like the problem could be in another place because of a division) How can i see all of a double ? I mean how do I print it so that 2.99999999999 doesn't show as 3.00 ?

Thanks

9. Adjust the precision of printf/cout.

printf("%.8f", val);

10. Thanks

The problem is a division 55344.6 divided by 360 on paper gives 153.735 exactly
but in C it returns 153.734999999999985

Then carries the error because it rounds to 153.73 as oposed to 153.74 which is what 153.735 would round to.
Is there a way to fix that?

11. Originally Posted by natalia
Thanks

The problem is a division 55344.6 divided by 360 on paper gives 153.735 exactly
but in C it returns 153.734999999999985

Then carries the error because it rounds to 153.73 as oposed to 153.74 which is what 153.735 would round to.
Is there a way to fix that?

When dealing with numbers that cannot be represented exactly in the number system you are using, I don't think it is reasonable to expect exact results.

55344.6 is not representable exactly as a binary floating point number (like float or double in C).

It is sometimes possible to derive at an estimate of the upper bound for errors in mathematical calculations, and in some cases it is useful to do so.

Regards,

Dave

12. Well it wasn't 55344.6 but 55344.6000000
Surely that is explicit enough?

I can understand that some divisions are not exact and cannot be expected to but isn't in a way 55344.6000000/360 the same as 4/2?
I mean in the sense that by hand you can arrive to an precise result..

It's not the same as 10.00/3.00 if you know what I mean

13. Well it wasn't 55344.6 but 55344.6000000
There is no difference between the two. Trailing 0s have no meaning in numerology.
No, this is about the binary representation of the number. An IEEE floating point value is simply not capable of representing this value.

14. doesn't make sense to me, I mean the fact that it is like that, complicates calculations unnecessarily..doesn't it? Or am I missing something? Our app doesn't even require amazing precision and it is already giving problems..

15. And how would you do it? Floating point numbers have been used for a few decades now. If they were so bad, they wouldn't be used anymore.

Sorry, the only tip I can give you is to increase the precision. double instead of float, long double instead of double (though the latter has no effect in MSVC++).