arthmetic bug

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello

I have a strange behavior of .net compiler
In my program I have 2 variables i and J (floats) i=1, j=0.
In a loop I add J and I : i +=

The result is quickly strange : i = 1.49999.99999

It works fine with VS 6

Someone can help me

Thanks
Lionel
 
Lionel,

You cannot just type " i += ;". That is not valid c++. I'm suprised you
even got it to compile!

I think you'll get more help if you post a short compile-able program that
demonstrates the weird behavior.

Rudy
 
lionel said:
Hello,

I have a strange behavior of .net compiler:
In my program I have 2 variables i and J (floats) i=1, j=0.1
In a loop I add J and I : i += ;

The result is quickly strange : i = 1.49999.999997

This is not a bug - it's the nature of floating point arithmetic. The
number 0.1 cannot be represented accurately in binary floating point.
Instead it's represented by a value just slightly smaller than 0.1. When
you add up a series of those you'll quickly see the accumulated error.
It works fine with VS 6 !

Just cioncidental, if everything else is the same. Whatever code you used
in VC6 is rouding the result to what you expect when you display it, while
the code in VC7{.1} is rouding it to a value you didn't expect. Neither is
wrong.

-cd
 
Sorry the right instruction is i += j


----- Rudy Ray Moore wrote: ----

Lionel

You cannot just type " i += ;". That is not valid c++. I'm suprised yo
even got it to compile

I think you'll get more help if you post a short compile-able program tha
demonstrates the weird behavior

Rud
 
Thanks for your answer

Never mind it's very strange I've tried to compile with float, double... with the same results

With VC6 it works fine with all types of variables

But if you need to do some mathematic algorithms you need an exact value and no approximation it's not acceptable and I don't want to ask the all team to move back to VS6 just for a rounding problem. I'll probably open a case at MS for that point

Regards
Lione

----- Carl Daniel [VC++ MVP] wrote: ----

lionel wrote
Hello
In my program I have 2 variables i and J (floats) i=1, j=0.
In a loop I add J and I : i +=

This is not a bug - it's the nature of floating point arithmetic. Th
number 0.1 cannot be represented accurately in binary floating point
Instead it's represented by a value just slightly smaller than 0.1. Whe
you add up a series of those you'll quickly see the accumulated error

Just cioncidental, if everything else is the same. Whatever code you use
in VC6 is rouding the result to what you expect when you display it, whil
the code in VC7{.1} is rouding it to a value you didn't expect. Neither i
wrong

-c
 
=?Utf-8?B?TGlvbmVs?= said:
But if you need to do some mathematic algorithms you need an exact
value and no approximation it's not acceptable

If you want do some "mathematic algorithmus" then you MUST first know that
computers are NOT ABLE to store (correct) mathematic values!

If you are not aware of this, then you should calculate your stuff by hand
(or use some third party tool like Maple / MathCad which handles most of
the complexity of computer numbers).

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp

Do you need daily reports from your server?
http://sourceforge.net/projects/srvreport/
 
Lionel said:
Thanks for your answer,

Never mind it's very strange I've tried to compile with float, double...
with the same results!

Of course!
With VC6 it works fine with all types of variables.

It just looks that way, because of different rounding. Have you tried what
happens for other values of i and j.

Lets try this out with decimal numers instead, using maybe 10 digits.
Calculate

j = 1 / 3; // 0.333333333

i = j; // 0.333333333

i += j; // 0.666666666

i += j; // 0.999999999


So, one third plus one third plus one third is what? Almost 1?

But if you need to do some mathematic algorithms you need an exact value
and no approximation it's not acceptable and I don't want to ask the all
team to move back to VS6 just for a rounding problem. I'll probably open a
case at MS for that point.

It would be better if you opened a book on Numerical Methods instead. It
would explain exactly what happens when you calculate with finite precision.
"Error propagation" was an important subject in the courses I took.


Bo Persson
 
Back
Top