2 Replies - 269 Views - Last Post: 25 September 2013 - 12:55 PM Rate Topic: -----

#1 keithgarry  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 63
  • Joined: 26-August 09

Floating-Point to Integral Conversions Magnitude vs Precision

Posted 25 September 2013 - 09:37 AM

I am currently reading C#5.0 In a Nutshell and came across this:

Quote

Implicitly converting a large integral type to a floating-point type preserves magnitude but may occasionally lose precision. This is because floating-point ypes always have more magnitude than integral types, but may have less precision. Rewriting our example with a larger number demonstrates this:


int i1 = 100000001;
float f = i1;
int i2 = (int)f;



I understand that explicit casts will result in data loss in some way. What I don't understand is how magnitude and precision actually work in this case. Could anyone shed some light on this?

Is This A Good Question/Topic? 0
  • +

Replies To: Floating-Point to Integral Conversions Magnitude vs Precision

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2886
  • View blog
  • Posts: 9,588
  • Joined: 12-December 12

Re: Floating-Point to Integral Conversions Magnitude vs Precision

Posted 25 September 2013 - 12:11 PM

The Locals Window is useful in this regard to see the difference in how the integer and float values are treated:

Posted Image

You can see how, for the float, the number is represented as the mantissa (1.0) which only has a certain range, the exponent (8) also has a certain range. This representation indicates how precision, but not magnitude, may be lost.

The following article may also be of interest.

How To Work Around Floating-Point Accuracy/Comparison Problems

This post has been edited by andrewsw: 25 September 2013 - 12:17 PM

Was This Post Helpful? 1
  • +
  • -

#3 Michael26  Icon User is offline

  • Radar love
  • member icon

Reputation: 332
  • View blog
  • Posts: 1,420
  • Joined: 08-April 09

Re: Floating-Point to Integral Conversions Magnitude vs Precision

Posted 25 September 2013 - 12:55 PM

Quote

You can try using the decimal type which stores numbers in decimal notation. Thus 0.1 will be representable precisely.

You wanted to know the reason:

Float/double are stored as binary fractions, not decimal fractions. To illustrate:

12.34 in decimal notation (what we use) means 1*101+2*100+3*10-1+4*10-2. The computer stores floating point numbers in the same way, except it uses base 2: 10.01 means 1*21+0*20+0*2-1+1*2-2

Now, you probably know that there are some numbers that cannot be represented fully with our decimal notation. For example, 1/3 in decimal notation is 0.3333333... The same thing happens in binary notation, except that the numbers that cannot be represented precisely are different. Among them is the number 1/10. In binary notation that is 0.000110011001100...

Since the binary notation cannot store it precisely, it is stored in a rounded-off way. Hence your problem.


Found this on SO

This post has been edited by Curtis Rutland: 25 September 2013 - 01:43 PM
Reason for edit:: added quote tag

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1