Float subtraction problem

Started by
8 comments, last by Doggan 19 years, 7 months ago
Damn, ive seen some weird stuff, but this bug takes the cake. Im using C++, compiler is Studio.NET. i have two variables of type float x and y. with the following values (values are loaded from a file) x = 34.635418 y = 34.635422 if i subtract them i get: float res = x - y; wait for it..... res = -3.8146973e-006 WTF?! THE ANSWER SHOULD NOT BE -3!!!! I have tried different compilers, all get the same result. PLEASE HELP - i bet its something really simple too.... Paul [Edited by - felisandria on September 19, 2004 3:15:12 PM]
Advertisement
it's -3x10^-6

e.g.

0.000003 (give or take a zero)

swearing doesn't help your case.
Welcome to scientific notation. You may have heard of it in all the high-school physics lectures you skipped.
Err, it says the answer is -0.000038146973, or -3.8146973 * pow(10, -6), which is correct. 34.635418 - 34.635422 is -0.00003

Edit: Heh, love how when I started typing there were no messages, but when I post there's 2 more.
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?
I think he should just uninstall his compiler and come back when he can handle basic math.
Quote:Original post by Ravuya
Welcome to scientific notation. You may have heard of it in all the high-school physics lectures you skipped.
ROOFLELOLROTFLMAOHAHALOL!!!!1~

High school physics was useless anyway.
rofl - i had completely forgotten :) Thanks though!
hey u guys, leave him alone.
he might be (like me) a young programmer who hasn't yet
done that math in school
Quote:Original post by jjac7212
hey u guys, leave him alone.
he might be (like me) a young programmer who hasn't yet
done that math in school

Yes, but that does not justify swearing in the post topic, and one is much less likely to receive constructive help/comments if people do not approve of your post language.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Beware: Subtracting two nearly identical floating point numbers is bad... mmmkay? ;)

This topic is closed to new replies.

Advertisement