Strange behaviour when using floats

  • Thread starter Thread starter Henrik Skak Pedersen
  • Start date Start date
H

Henrik Skak Pedersen

Hi,

I have the following very simple code snippet:

line 1: float f;
line 2: f = 100000.99f;

Why is f: 100000.992 after line 2? I would assume it to be 100000.99.

Cheers
Henrik.
 
Hi Hans,

Thank you for your reply. So I guess that decimal is my choice then.

Cheers
Henrik.
 
Henrik said:
Hi Hans,

Thank you for your reply. So I guess that decimal is my choice then.

Cheers
Henrik.

You can use a double if you just want better precision, but still want
the performance of a type that is supported by the ALU of the processor.

A decimal still doesn't represent values exactly, but you get something
like 100000.990000002 rather than 100000.992.
 
You can use a double if you just want better precision, but still want
the performance of a type that is supported by the ALU of the processor.

A decimal still doesn't represent values exactly, but you get something
like 100000.990000002 rather than 100000.992.

Both double and decimal represent exact values - just different sets
of values. And decimal certainly *can* represent 10000.992 exactly.

Jon
 
Obviously I meant a double.
still doesn't represent values exactly, but you get something

Both double and decimal represent exact values - just different sets
of values.

What do you mean by that? A double is a floating point type, and
approximates most values.
And decimal certainly *can* represent 10000.992 exactly.

A decimal stores 100000.99 as 100000.99, not as 100000.992.
 
Göran Andersson said:
Obviously I meant a double.

Sorry, it wasn't obvious to me. I thought you really were casting
aspersions on decimal :)
What do you mean by that? A double is a floating point type, and
approximates most values.

Decimal is a floating point type too - just a floating decimal point
instead of a floating binary point. Both types have a set of numbers
they can represent exactly. You can't represent 1/3 exactly in decimal
any more than you can represent 1/5 exactly in binary.

The reason that decimal "feels" precise whereas binary "feels"
approximate is that humans tend to use a decimal system - we tend to
think that 1/5 is a more "normal" number than 1/3. When you look at it
in a mathematical sense, decimal has more precision and less range than
double, but both are just sets of numbers with conversions and
operations which will approximate the accurate value to the nearest
value in the target set.
A decimal stores 100000.99 as 100000.99, not as 100000.992.

Indeed - I didn't check the original post, just yours.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top