Floating point representation

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Kyosaur
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 78
Joined: Tue Jul 13, 2010 2:00 am
Favorite Gaming Platforms: PS2,PS3,NDS
Programming Language of Choice: C++

Floating point representation

Post by Kyosaur »

I've been reading a TON regarding this topic, yet im still very confused on the matter. How are floating point numbers represented? Everything i've read has basically told me the same exact thing, and has never showed an example that i was able to understand! I feel as though i've read so much differently stated information, that nothing make sense/is correct anymore. This my current understanding, please correct what isnt right.

The MSB (2^31 for 32bit system) is responsible for the negative/positive sign like it is with normal signed integers. The next 8 bits are a biased exponent (this is what i understand to be val - 127? With a range of 1-254 (0/255 are for special numbers)). The remaining bits, the mantissa/significand, are for the fraction data.

The thing that im seriously having problems with is CONVERTING. Im trying to make sense of a floating number, but i just get data that doesnt remotely make sense to me. These are my issues with converting (i know some of the stuff is going to sound stupid- i dont mind appearing stupid as long as i learn):
  • Converting FROM float:
    • Code: Select all

      Original number: 13.37
      Binary number: 01000001010101011110101110000101
      
      MSB: 0.
      Biased Exponent: 130 (10000010).
      Mantissa: 5630853 (10101011110101110000101).
      
      Mantissa * 10^130 will never equal our desired value "13.37" no matter where we put the decimal (or radix point). This is seriously confusing!
    Converting TO float:
    • My main issue with converting TO a float value comes from lack of information + sleep deprivation. The first bit of information is where does the decimal point start? I've seen scientific notation denoted tons of different ways, so which one is correct here? If i store 1337 inside of the mantissa where does the decimal go? (Is it safe to assume it goes 1.337? I've seen it go 0.1337). Its important to know where the decimal goes, other wise it could be off when multiplying :'(.
    • The biased exponent is confusing. Assuming the exponent is implicitly placed after the one (1.337) i want to multiply it by 10^1. This is normally would be straight forward, except we have the bias to account for, which makes me unsure HOW i should store the value 1. Do i store it as 0b1 (0b1 - 127 = -126) or do i store it as 0b10000000 (128 - 127 = 1). I might be over thinking this part. I believe it to be the first, but it feels so unnatural and confusing (especially when READING).
Can someone tell me what i am doing wrong, and answer the questions above? I know this is an advanced subject, but its bothering me that i cant make sense of floating point numbers (especially since i have a good understanding of binary!).
Image
User avatar
szdarkhack
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 61
Joined: Fri May 08, 2009 2:31 am

Re: Floating point representation

Post by szdarkhack »

Here is a wikipedia link of the conversion process, both FROM and TO float (single precision/32bits):
http://en.wikipedia.org/wiki/Single_pre ... y32_format

My guesses as to what might be giving you wrong results are the following:
-Are you normalizing the numbers correctly? That means that there must be ONE digit on the left of the decimal point, no more than that.
-Did you remember to account for the implied bit 24?
-Do you convert the fractional part of the number to binary correctly?

Any of these could be the causes, but check out the link, it gives examples both ways. I hope that will clear this up for you :)
User avatar
Kyosaur
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 78
Joined: Tue Jul 13, 2010 2:00 am
Favorite Gaming Platforms: PS2,PS3,NDS
Programming Language of Choice: C++

Re: Floating point representation

Post by Kyosaur »

Thanks for the link, but im still very confused xD. Im just gonna keep doing some digging until i get it. If anyone actually understands what im doing wrong, or even the process itself, please try to explain it to me :( lol. To be completely honest im just tired of reading about this subject- its exhausting! Curse my need to know how stuff works :evil:.
Image
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Floating point representation

Post by short »

Kyosaur wrote:To be completely honest im just tired of reading about this subject-.
Take a break from it for a week. Do something productive, such as...... watching the entirety of Battlestar Galactica season 1-4, then come back too it. :lol:

Seriously, sometimes a little break helps. When you come back it will be easier to digest, as it will be the second time and your brain will be more familiar with it (and you won't be burnt out).
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: Floating point representation

Post by qpHalcy0n »

...or take a shit.

Shits help the thought process. Not really kidding....
Post Reply