Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working out floating point numbers in base 2, 10 and 16

I was reading across my notes and came across the following:

For every real number there are various ways of representing
it in such a way. Therefore, computers fix two parameters (so
they do not need to be stored, and arithmetic is more
convenient):

- the base b (normally, it is 2, 10 or 16) and
- the position of the decimal (or binary) point (by normalising
the mantissa such that it satisfies 1/b ≤ m < 1)

Example: Normalised representations for r := 12.25 are,
- for b = 2, r = 1 × 0.110001 × 2^4,
- for b = 10, r = 1 × 0.1225 × 10^2 and,
- for b = 16, r = 1 × 0.C4 × 16^1.

How do you go about working out the floating point numbers in base 2, 10 and 16 for the value 12.25. I'm not too sure at how the lecturer arrived at his answers for b = 2, b = 10 and b = 16.

like image 979
methuselah Avatar asked Jan 17 '23 21:01

methuselah


2 Answers

From the examples, it seems like your lecturer's definition of "normalized" is to represent the number as +1 or -1 multiplied by some x multiplied by the base raised to an integer power, where x is the largest value less than 1 for which the product equals the represented number. Also, x is represented as a numeral in the base.

E.g., consider 12.25 in base 2. Sticking to base 10 for the moment, we could represent 12.25 as 1×12.25×20 or 1×6.125×21 or 1×3.0625×22 or 1×1.53125×23 or 1×.765625×24 or 1×.3828125×25, and so on. Of these, we can see that .765625 is the largest value less than 1 that fits the form. So, we represent 12.25 as 1×.765625×24. Then we need to convert .765625 to base 2.

You have probably covered that in previous lessons, but we can do it like this: Multiply .765625 by 2 (to get 1.53125) and separate the integer part (1) from the fraction (.53125). Multiply the fraction by 2 (1.0625) and separate again (1 and .0625). Repeat with the new fraction (0 and .125). Continue repeating until the fraction is zero or you have as many digits as you want: 0 and .25, 0 and .5, 1 and 0. List the integers you got: 1 1 0 0 0 1. Now the base-two numeral you want is a period followed by those digits: .110001. So, 12.25 in base 2, normalized according to your lecturer's definition, is 1×.110001×24.

A rule for finding the right value of x could be this: Start with an exponent of 0. If x is larger than 1, divide it by the base and add one to the exponent. If x is less than 1/base, multiply it by the base and subtract one from the exponent. Repeat this until x is between 1/base and 1 (including 1/base but excluding 1, so stop if x equals 1/base).

For 12.25 and decimal: Start with exponent 0. Divide 12.25 by 10 (getting 1.225) and increment the exponent to 1. Divide again (.1225) and increment the exponent to 2. Now we stop because .1225 is between 1/10 and 1.

For 12.25 and base 16: Start with exponent 0. Divide 12.25 by 16 (getting .765625) and increment the exponent to 1. Now stop because .765625 is between 1/16 and 1.

To convert .765625 to base 16: Multiply .765625 by 16 to get integer 12 (digit C) and fraction .25. Multiply .25 by 16 to get integer 4 and fraction 0. The fraction is 0, so stop. The base-16 numeral is .C4, so the whole form is 1×.C4×21.

Sometimes, other definitions of "normalized" will be used. Commonly, instead of adjusting the x to be between 1/base and 1, we adjust the x to be between 1 and b.

like image 176
Eric Postpischil Avatar answered May 24 '23 09:05

Eric Postpischil


We're trying to represent the number 12.25 in various bases. So without normalizing.

For Binary:

   12    - > 1100
   0.25  - > .01  (1 * (1/2)^2)
so 12.25   = 1100.01

For Hex :

   12    - > C
   0.25  - > .4   (4 * (1/16)^1)
so 12.25   = C.4 

and then the decimal point has been shifted in conjunction with an exponent term.

Hope that helps.

I guess this isn't an answer because I'm not describing any method. So here's some links to discussions at MathForums on the topic.

Long Division with Binary Numbers

Floating Point Binary Fractions

Dr. Math's FAQ on Bases

I'm still looking for a good guide to Base 16 Floating point division

Base Convert and Digit Convert are online base converters that can work with floating point numbers.

The sites use Javascript so it is possible to get to the algorithm being used.

like image 25
Nishant Sharma Avatar answered May 24 '23 10:05

Nishant Sharma