Help with fixed point numbers

digithree

Help with fixed point numbers

Post by digithree » Tue Mar 29, 2011 10:15 pm

If sinLerp and other trig functions return signed values in 4.12, shouldn't one of those bits (the most signifigant bit usually isn't it) be the sign bit? So shouldn't it be 3.12 or 4.11 for a signed number?

mtheall
Posts: 210
Joined: Thu Feb 03, 2011 10:47 pm

Re: Help with fixed point numbers

Post by mtheall » Tue Mar 29, 2011 10:50 pm

Floating point numbers use a sign bit. Fixed point numbers do not. They are sign-extended like integers (and they are indistinguishable from integers). You have to shift them appropriately depending on what operations you perform on them (addition, subtraction, multiplication, etc.), and what the other operand is (an integer or fixed point with different decimal position, etc).

zeromus
Posts: 212
Joined: Wed Mar 31, 2010 6:05 pm

Re: Help with fixed point numbers

Post by zeromus » Wed Mar 30, 2011 12:44 am

i'm sorry you disagree with the way that fixed point numbers are named. but that's how it is. for what it's worth, there is no real standard and there is some ambiguity, although the situation as it currently stands is most typical. But of the two alternatives you supplied, it is certainly more like 3.12 than 4.11. However, in truth, it is 4.12

digithree

Re: Help with fixed point numbers

Post by digithree » Tue Apr 05, 2011 3:07 pm

Well it seems that negative numbers are stored in signed integers using 2s compliment. Since fixed point numbers are just integers with a scale factor (for a x.y fixed point number the scale is the entire number / 2^y ) they work the same as integers with regards negative number storing.

For a nibble (half byte) here's a few numbers and how 2s compliment negative numbers are stored

Code: Select all

binary      decimal
------      -------
0000        0
0001        1
1111        -1
1110        -2
It matters what the size of the type is. For each type size (nibble has 4bits, byte 8, word 16, etc.) the MSB (most significant bit, i.e. left most bit) is the sign bit. So for the negative of a binary number, just invert the bits and set the MSB. For a number to be interpreted as positive the MSB must be unset (i.e. 0)

This means number range for a 16bit signed integer is 32,767 to -32,768, or 0111 1111 1111 1111 to 1000 0000 0000 0000 in binary.

With regards to whether or not a signed integer has a sign bit, surely it does have a sign bit. For those of us who are aren't as familiar with low level types of which higher level types are constructed, it does seem a bit confusing that a signed 16bit fixed point number can be described to be 4.12 and yet only have a maximum positive value of "3.12" number of bits but of course the 16th bit is the sign bit (although perhaps this is the wrong terminology).

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: Help with fixed point numbers

Post by elhobbs » Tue Apr 05, 2011 4:35 pm

If there was a "sign bit" then there would be 2 values for 0. as you said it is 2's compliment - where it just so happens that the high bit can be used to determine if a value is negative.

zeromus
Posts: 212
Joined: Wed Mar 31, 2010 6:05 pm

Re: Help with fixed point numbers

Post by zeromus » Tue Apr 05, 2011 11:08 pm

integers have sign bits. a 32 bit integer is 32 bits and has a sign bit. a 4.12 fixed point integer has 4 integer bits and one of those is a sign bit. a normal integer is 32.0 fixed point. all of this is completely sensible and consistent. you don't ask for an integer to be called 31.0 or 1.31.0 or for anyone to claim that it has only 31 integer bits and one sign bit. it has 32 integer bits and 1 sign bit, just like you have five fingers and one of them is a thumb.

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: Help with fixed point numbers

Post by elhobbs » Tue Apr 05, 2011 11:37 pm

This thread is becoming useless, but I must disagree with you zeromus. It is possible to represent ints with a sign bit such that positive 1 would be 0x00000001 and -1 would be 0x80000001. In this case it is not a dedicated sign bit and zero does not need to be normalized and numbers cannot be negated by flipping the sign bit. However, as I said you can tell if something is negative by looking at the high bit.

zeromus
Posts: 212
Joined: Wed Mar 31, 2010 6:05 pm

Re: Help with fixed point numbers

Post by zeromus » Wed Apr 06, 2011 12:23 am

it is possible to represent poodles with ints as well. but obviously thats not what i meant. when i said int, i meant a normal 2s complement integer such as we use every day for various things. im saying how things _are_ not how they _might be_ if youre choosing to be contrarian.

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: Help with fixed point numbers

Post by elhobbs » Wed Apr 06, 2011 1:44 am

Yet, floating point numbers have a sign bit and behave similar to my example and integers use 2's compliment and do not. I suppose it is not important and is mostly a matter of semantics, but I think it confuses the issue... similar to me refusing to let this go... Done.

WinterMute
Site Admin
Posts: 1856
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

Re: Help with fixed point numbers

Post by WinterMute » Wed Apr 06, 2011 3:12 am

The term "sign bit" has a very specific meaning which does not include the sign offset in an integer value. Floating point representation has a sign bit as do some DSP chips, most processors use 2s complement to represent signed numbers. This is neither semantics nor unimportant and drives me nuts when people perpetuate this kind of misinformation.
Help keep devkitPro toolchains free, Donate today

Personal Blog

Post Reply

Who is online

Users browsing this forum: No registered users and 26 guests