Mantissa/Exponent

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 8681 Views - Last Post: 17 March 2010 - 04:51 PM Rate Topic: -----

#1 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 375
  • Joined: 14-December 08

Mantissa/Exponent

Posted 02 January 2010 - 07:44 PM

Well...This is the chart of the range of each of the floating point type: Float, double and decimal.

Float: 32bits, 7digits, 1.5 x 10E-45 to 3.4 x 10E38

Double: 64bits, 15-16 digits, 5.0 x 10E-324 to 1.7 x 10E308

Decimal: 128bits, 28-29 decimal places, 1.0 x 10E-28 to 7.9 x 10E28


Well...My question is, take float as an example, 10E38, does it mean that 10 is an exponent, which multiplies by itself 38 times? That's 10x10x10x10x10x10x10x10x10x10...etc. So float with 1.5 x 10E-45 to 3.4 x 10E38 means that the decimal places or digits can range from 1.5 / 10 x (45 times of 10 itself) to 3.4 x 10 x (38 times of 10 itself), but only 7 digits of them are accurate?

Is This A Good Question/Topic? -1
  • +

Replies To: Mantissa/Exponent

#2 QuackWare  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 02-January 10

Re: Mantissa/Exponent

Posted 02 January 2010 - 09:21 PM

10E38 is the same as 10^38 so yeah 10 multiplied by itself 10 times
Was This Post Helpful? 0
  • +
  • -

#3 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 84
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: Mantissa/Exponent

Posted 03 January 2010 - 06:57 AM

View Postkenryuakuma, on 3 Jan, 2010 - 01:44 AM, said:

Well...My question is, take float as an example, 10E38, does it mean that 10 is an exponent, which multiplies by itself 38 times?


Ten times itself 38 times is 10^38, or 1E38, or 10E37.

The E notation stands for (10^). So 10E38 = 10 * 10^38, which is 1E39.

http://en.wikipedia....tion#E_notation
Was This Post Helpful? 0
  • +
  • -

#4 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 375
  • Joined: 14-December 08

Re: Mantissa/Exponent

Posted 03 January 2010 - 05:47 PM

Well...When it says something like 7 digits, does it mean that out of the 38 digits of the numbers with float type, and only 7 digits are accurate?
Was This Post Helpful? 0
  • +
  • -

#5 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Mantissa/Exponent

Posted 03 January 2010 - 06:18 PM

View Postkenryuakuma, on 3 Jan, 2010 - 06:47 PM, said:

Well...When it says something like 7 digits, does it mean that out of the 38 digits of the numbers with float type, and only 7 digits are accurate?


It a way, yes. A floating point number will not have all of the 38 digits in it. It will have the most significant digits for that number. The reason is that memory is finite and a floating point number could be infinite. Examples of infinite are (1/3), repeating decimals, and PI, irrational numbers. If you tried to store all of the digits of one such number you will fill up all of your memory. That is where the rounding error comes in. If, for example, you represent, (1/3) as a double it would be 0.333 to the number of decimal places a double is accurate to. Now, if you take that result and multiply it by 3 you would expect to get 1 but you won't. You will get 0.999 to the number of decimal places a double is accurate to.
Was This Post Helpful? 0
  • +
  • -

#6 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 375
  • Joined: 14-December 08

Re: Mantissa/Exponent

Posted 04 January 2010 - 06:49 PM

Would you please talk a little more about this in detail SixOfEleven because I really wanna confirm if what I learn is really the solid information.

http://msdn.microsof...y/9ahet949.aspx
Was This Post Helpful? 0
  • +
  • -

#7 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Mantissa/Exponent

Posted 05 January 2010 - 12:50 PM

Sorry, this is going to be a little long. I'm just trying my best to explain this subject thuroughly.

The problem is computers are finite and math is infinite. There is no true way to express something that is infinite in a computer. There is a limit to what a computer can hold. Even with a string, you can not have a string that is inifinite because it would fill up all of the memory in the computer and require more to hold all of the data. True, we have come along way from where we were in abilities of representing data but there will always be a limit.

When a person designs a programming language they have to figure out how to store different data types, like integers and floating point numbers. The way they are stored physically in memory was for a time linked to the processor. For example, integers were represented using 16 bits. The first bit was used as a flag for if the number was positive or negative. As languages and processors evolved the size of variables in memory changed. In C# when you declare an int it uses 32 bits, that is why you often see Int32.Parse.

Decimal numbers are not as easy to represent in memory as integers. Integers are easy because they can be easily be converted to and from binary, what the computer actually understands and uses to store data. Decimal numbers have a different way of storing their values. (It's been a long time since I've looked at this in computer architecture so I'm not going into it in depth.) But basically decimal numbers are represented in memory using two parts. The first part is the number of digits the decimal number is accurate to. The second part holds the exponent for the number. That is why the link you posted says that a float is accurate to 7 decimal places with an exponent and a double is accurate, more or less, to 15-16 decimal places. Using this format of storing numbers is more efficient for the program and the processor. Modern processors have floating point math processors to help speed up floating point math. Like I said, integer math is much easier because it is already in binary format.

If you can use an integer type to hold a value there will be no margin of error. There can however be overflow and this is not caught by the compiler or even the executable. What an overflow error is, if you take the maximum value of integer and add 1 to it the variable will go negative if it is signed or 0 if it is unsigned. You might be wondering why this happens. Well, like I mentioned above variables have only a finite number of bytes in memory. In an integer number you will have an overflow if you exceed the maximum or minimum values of the variable. For example, an 8 bit signed number is represented with the first bit representing the sign of the number and the other 7 bits representing the powers of two for the number. So, 127 is represented in binary as 01111111 and 1 is represented by 00000001. When you preform binary addition if both bits are 0 the remain 0. If one bit is 1 and the other is 0 the bit remains 1. Finally, if both bits are 1, the new bit is 0 and 1 is added to the next bit. If you preform the addition of 127 and 1, in binary you would get the following 10000000. Which to you is 128 but to the langauge it is actually -127. You caused an overflow and the variable exceeded its maximum. This is where the Y2K bug came from initially. When some programmers were modelling data they used two characters 00-99 to represent the year in databases. What happens when you go from year 99 to 100? How is your program going to handle that? When you are using integer types there is always the chance of overflow because the size of the number in memory is constant. I'm not sure about C# but in C there was the possibility than an overflow could corrupt other data if the first bit in the variable caused an overflow which would kill other programs.

Floating points, since they only have finite space, also have issues. The issue with floating points is that they only have a certain degree of accuracy. This is because of the way that they are stored in memory. The decimal part of the number can only have so many digits just as an integer has a maximum and minimum value in memory so do decimal numbers. Because of this you can have rounding errors. One example of a rounding error is the following:

double result;

result = Math.Sqrt(2);
Console.WriteLine("{0}", result);
result *= 2;
Console.WriteLine("{0}", result);



If you take the square root of 2 and store that value in the variable result and then multiply it by 2 shouldn't you get 2 back? Try it, you won't. The is a rounding error. Sqrt(2) is infinite. The value continues on forever, just like PI does. When you get the value of Sqrt(2) it is only an approximation of what Sqrt(2) is actually. A computer has no way of holding all of the decimal places of Sqrt(2). You could techincally calculate its value until there is no memory left and the calculation will keep on going forever. When you multiply Sqrt(2) by 2 you actually get a value greater than 2.

Another problem with floating point numbers is working with large and small values together. For example if you were to add 1 + 10^32 and 1 the 1 wouldn't register in the floating point variable. The 1 would not be significant enough to effect the floating point variable. In math though, it would actually effect the number.
Was This Post Helpful? 1
  • +
  • -

#8 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: Mantissa/Exponent

Posted 05 January 2010 - 04:32 PM

View PostSixOfEleven, on 5 Jan, 2010 - 09:50 PM, said:

double result;

result = Math.Sqrt(2);
Console.WriteLine("{0}", result);
result *= 2;
Console.WriteLine("{0}", result);


Very nice writeup, except this should use Math.Pow(result, 2) or somthing similar, since now you're getting 2*Sqrt(2).
Also when you run this it will show the end result as plain 2, since Console.WriteLine also rounds numbers, but if you break the program at the last line you can clearly see the answer isnt't exactly 2, which was the point you were making.
Was This Post Helpful? 0
  • +
  • -

#9 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Mantissa/Exponent

Posted 05 January 2010 - 04:38 PM

View PostRenagado, on 5 Jan, 2010 - 05:32 PM, said:

Very nice writeup, except this should use Math.Pow(result, 2) or somthing similar, since now you're getting 2*Sqrt(2).
Also when you run this it will show the end result as plain 2, since Console.WriteLine also rounds numbers, but if you break the program at the last line you can clearly see the answer isnt't exactly 2, which was the point you were making.


Thanks.

Well, it didn't round when I ran the test program. But you are right about Console.WriteLine rounding things. It happened when I tested (1/3) and then the result * 3. You could use the formatting of the WriteLine method to get the decimal places. Didn't feel like doing that though.
Was This Post Helpful? 0
  • +
  • -

#10 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 375
  • Joined: 14-December 08

Re: Mantissa/Exponent

Posted 07 January 2010 - 10:09 PM

Well...Sorry for keeping bugging you, 6of11 because you are a good teacher which I could rely on for help. So what does it mean by this from MSDN:

FLOAT

1.5e−45 to 3.4e38
Was This Post Helpful? 0
  • +
  • -

#11 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Mantissa/Exponent

Posted 10 January 2010 - 12:39 PM

1.5e−45 to 3.4e38 are the maximum/minimum values for a float. This is a little complicated to explain so bear with me. I will start with 3.4e38. The means that the value can be positive or negative. So, the negative value would be -3.4e38 and the positive would be +3.4e38. Since the value after e is positive the values would go like this:

e0 = 1
e1 = 10
e2 = 100
e3 = 1000
etc

So, 3.4e38 would be a really large positive value and -3.4e38 would be a really large negative value.

Now, 1.5e−45, would range between -1.5e-45 and 1.5e45. Since e is negative the value would go like this:

e0 = 1
e-1 = 0.1
e-2 = 0.01
e-3 = 0.001
e-4 = 0.0001
etc

As you can see this will eventually be values between -1 and 1 when the values after the e- are larger.
Was This Post Helpful? 0
  • +
  • -

#12 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 375
  • Joined: 14-December 08

Re: Mantissa/Exponent

Posted 10 January 2010 - 08:16 PM

Got it and I am so grateful to your help, again really appreciate it.
Was This Post Helpful? 0
  • +
  • -

#13 Guest_JakeL*


Reputation:

Re: Mantissa/Exponent

Posted 17 March 2010 - 07:12 AM

I don't understand what is up with people going back and forth with the ranges for floats/doubles... I.E.
Sometimes you see ranges for floats as -(2^31) to ((2^31)-1) or -2147483648 to 2147483647 and other times you see 1.5e−45 to 3.4e38 . Now if you write those out +1.5e-45 would be 000000000000000000000000000000000000000000015 to +3.4e38 which is 340,000,000,000,000,000,000,000,000,000,000,000,000. Now isnt the number 340,000,000,000,000,000,000,000,000,000,000,000,000 Much Much bigger than 2147483647.. So if floats max size is 2147483647 then how could you ever store 340,000,000,000,000,000,000,000,000,000,000,000,000 in a float?
Was This Post Helpful? 0

#14 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Mantissa/Exponent

Posted 17 March 2010 - 10:45 AM

Quote

-(2^31) to ((2^31)-1) or -2147483648 to 2147483647


Those values are for integer numbers which are different than float/double numbers. They work quite differently than floats and are stored differently internally. They reason is that they are already in binary, the computer's format, and can be worked with quite easier than base 10 numbers, decimals. The maximum and minimum values an integer can hold is determined by the number of bytes are used to store it. The one you posted uses 4 bytes, or 32 bits. That is why you have those values. A 16 bit number would look like this:

-(2^15) to ((2^15)-1)

A 64 bit number would look like this:

-(2^63) to ((2^63)-1)

Floats are broken up into two parts: the mantisa and the exponent. The exponent holds the powers of ten for the float and the mantisa holds a certain degree of accuracy for the float about 7 for a single percision number, ie float in C#, or 15 for a double percision number, ie double in C#. So when you start to work with extremely large numbers, or extremely small numbers, there is a loss of percision. So, if you add a very small number to a very large number, the small number will be ignored in a float. The format of a floating point number also gives it one extra bit of percisions.

Look up the IEEE 754 standard for more information on how floating point numbers are stored.
Was This Post Helpful? 0
  • +
  • -

#15 Guest_Guest*


Reputation:

Re: Mantissa/Exponent

Posted 17 March 2010 - 11:18 AM

SixofEleven thank you for your response. It was very informative. I just had a few more questions if you don't mind. Just to kinda clear the merkiness for me. So when you see list of data types for C in particular and they list float as 4 bytes what do they actually mean ? How are these individual bits allocated for a float? Is it like 7 bits for the mantissa and 8 bits for the exponent and 1 bit for the sign? Also, the way you explained it how does the 3.4e38 relate? Like, if that value is for the max that the exponent can be then how is the exponent stored? if you did the max value for your exponent say +3.4e38 then would the integer 38 be stored and thats it for the exponent? and say your mantissa was 1.2345 then im guessing that just gets stored as an integer as well and is the . symbol stored as an integer or is that merley an asthetic when printing a float? Also if I did something like
float value = 123,450,000,000,000,000,000,000,000,000,000,000,000f im guessing that is within range? Also I will read the IEEE 754 standard thank you for the reference.. And if my question is a bit of a ramble I apologize I dont expect you to sift through it and try and answer everything perfectly, I understand its kinda unorganized. Also from what im gathering It seems like the float is just a container/class with members like

int mantissa;
int exponent;
int signed;
etc etc.....
??
Was This Post Helpful? 0

  • (2 Pages)
  • +
  • 1
  • 2