# Floating-Point to Integral Conversions Magnitude vs Precision

Page 1 of 1

## 2 Replies - 775 Views - Last Post: 25 September 2013 - 12:55 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=329985&amp;s=cca7713cb28514f3b3e24160597c4157&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 keithgarry

Reputation: 9
• 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

• I'm not here to twist your niblets

Reputation: 4276
• Posts: 15,682
• 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:

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

### #3 Michael26

Reputation: 376
• Posts: 1,559
• 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