Hello.
I have seen that System.Double has some arithmetic errors when making some
operation like this:
Dim x As System.Double
For x = 0.01 To 0.5 Step 0.01
Debug.WriteLine(x)
Next
The counter x sums 0.01 in every loop and everthing go right until 0.05, but
at the sixty loop the result it 0.060000000000000005. Therefore, following
operations give me a wrong result. I think its related to machine's
arithmetic processor, and I expect this can happen regardless of language or
platform when handling floating-point variables.
I make two questions:
1. why does this error occurs?
2. once that fixed-point variables like System.Decimal don't generete this
kind of error, what are the implications if I discard the use of
floating-point variables and always use fixed-point ones in my applications?
Thank you in advance.
--
Celio C. J.
Klaus L?ffelmann - 28 Oct 2003 23:40 GMT
C?lio,
hope, my English is sufficiant enough - I'll give it a shot anyway.
Rounding errors are a natural phenomen when converting values from one
number system to another. Think of 10/3 for example: In the decimal system
that is 3.333333... (an infinit fraction), in the number system based on 3
(is it called tercial system?) it's simply 4.
The decimal type embarks on a different strategy than single or double in
representing fractions and therefore guarantees correct conversation to the
decimal system without rounding errors.
Hope that helps
Klaus
> Hello.
>
[quoted text clipped - 23 lines]
> --
> Celio C. J.
Klaus L?ffelmann - 28 Oct 2003 23:41 GMT
C?lio,
hope, my English is sufficiant enough - I'll give it a shot anyway.
Rounding errors are a natural phenomen when converting values from one
number system to another. Think of 10/3 for example: In the decimal system
that is 3.333333... (an infinit fraction), in the number system based on 3
(is it called tercial system?) it's simply 4.
The decimal type embarks on a different strategy than single or double in
representing fractions and therefore guarantees correct conversation to the
decimal system without rounding errors.
Hope that helps
Klaus
> Hello.
>
[quoted text clipped - 23 lines]
> --
> Celio C. J.
Marc Scheuner [MVP ADSI] - 29 Oct 2003 08:47 GMT
>I have seen that System.Double has some arithmetic errors when making some
>operation like this:
>The counter x sums 0.01 in every loop and everthing go right until 0.05, but
>at the sixty loop the result it 0.060000000000000005. Therefore, following
>operations give me a wrong result.
>1. why does this error occurs?
Floating point numbers are always just an approximation - they can
never give you an exact value. After all, the main processor works in
a binary system with a limited number of significant digits - it's
always a trade-off between range of numbers, and accuracy.
If you do need exact numbers, but you can limit yourself to a smaller
"range" of numbers, you should always use System.Decimal (e.g. for
financial apps etc. where every penny being rounded off would hurt
someone). They are more demanding in terms of storage (space) and
processing (performance), but they get the job done.
Same goes for databases - if you use MS SQL Server, you should use
DECIMAL for currency fields - NEVER use FLOAT !
Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
Jon Skeet [C# MVP] - 29 Oct 2003 09:04 GMT
> Floating point numbers are always just an approximation - they can
> never give you an exact value.
I wouldn't put it quite like that - they *do* have exact values, but
they can't exactly represent *every* value.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Marc Scheuner [MVP ADSI] - 29 Oct 2003 14:54 GMT
>> Floating point numbers are always just an approximation - they can
>> never give you an exact value.
>
>I wouldn't put it quite like that - they *do* have exact values, but
>they can't exactly represent *every* value.
Well, just try to make a number in the area of 10^308 be exact, with
only 15 significant digits....... ;-)
Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
Jon Skeet [C# MVP] - 29 Oct 2003 15:12 GMT
> >> Floating point numbers are always just an approximation - they can
> >> never give you an exact value.
[quoted text clipped - 4 lines]
> Well, just try to make a number in the area of 10^308 be exact, with
> only 15 significant digits....... ;-)
10^308 itself is exact and only has one significant digit :)

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Marc Scheuner [MVP ADSI] - 30 Oct 2003 09:15 GMT
>> Well, just try to make a number in the area of 10^308 be exact, with
>> only 15 significant digits....... ;-)
>
>10^308 itself is exact and only has one significant digit :)
Wise guy ;-) What about 10^308 / 31 or something like that?? ;-)
Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
Jon Skeet [C# MVP] - 30 Oct 2003 10:38 GMT
> >> Well, just try to make a number in the area of 10^308 be exact, with
> >> only 15 significant digits....... ;-)
> >
> >10^308 itself is exact and only has one significant digit :)
>
> Wise guy ;-) What about 10^308 / 31 or something like that?? ;-)
Then indeed you won't get the *same* exact figure - but the figure
which is closest to it is an exact figure in itself.
To demonstrate what I mean with simpler examples, think about a
notional decimal type with 2SF and the rational number 1/3. 1/3 isn't
exactly representable as a decimal (however many significant figures
you've got), but the number 0.33 is still an exact number. There's
nothing approximate about 0.33 in itself - it's only when you compare
it to the "desired" number of 1/3 that approximations come in.
I would put it this way:
Every floating point number is an exact number, but it may only be
approximately equal to the mathematical result of the calculation
performed to reach it.
Do you see what I mean?

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jon Skeet [C# MVP] - 29 Oct 2003 09:05 GMT
Célio Cidral Junior <ccidral@expresso.com.br> wrote:
> I have seen that System.Double has some arithmetic errors when making some
> operation like this:
[quoted text clipped - 8 lines]
> at the sixty loop the result it 0.060000000000000005. Therefore, following
> operations give me a wrong result.
<snip>
> I make two questions:
> 1. why does this error occurs?
See http://www.pobox.com/~skeet/csharp/floatingpoint.html
> 2. once that fixed-point variables like System.Decimal don't generete this
> kind of error, what are the implications if I discard the use of
> floating-point variables and always use fixed-point ones in my applications?
See http://www.pobox.com/~skeet/csharp/decimal.html
But note that decimal isn't a fixed point type - it's just that it's a
floating *decimal* point type instead of a floating *binary* point
type.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too