| |
terric Account closed
Registered: Feb 2009 Posts: 47 |
Sane or bad decimal format (sharing?)
I latetely finished some code.
And after many hours of thinking i came to the conclusion that saving a value below 1 like 0.75 could be done in a byte. You could almost make a 1.0 by setting all 7 bits in the byte.
My format: MSB is signed bit and the others below has the values 0.5,0.25,0.125 and so on. I got this when repeating mathematics and thought that by describing a value close to real value by setting the bits correct would make a difference.
What if we now have the value of 0.75?
We can describe it as 1/2 + 1/4 yes, so if we edit according bits 6,5 to 1 and the rest to 0 then we have a byte with the value of 0.75 represented as 01100000.
Ideas, good or bad ? ( I have a working times engine unrolled for this byte )
terric |
|
| |
chatGPZ
Registered: Dec 2001 Posts: 11386 |
welcome to fixed point math =)
(and yes its not only a good idea, its the common solution on platforms with no FPU =P) |
| |
WVL
Registered: Mar 2002 Posts: 902 |
the discovery of 1/256th ;) Ofcourse this is the only sane way to describe values with which you can do fast math on c64. Disadvantage : try to write some 'common' normal decimal values like 0.1 in this format.. (ok, ofcourse you can get close, but you'll never get there) |
| |
chatGPZ
Registered: Dec 2001 Posts: 11386 |
ofcourse the question is... are we really talking about representing *decimal*, or rather *fractions* :) |
| |
Skate
Registered: Jul 2003 Posts: 494 |
if you need more sensitivity in fractions, you can use all of the 8 bits. so 0.75 equals to %11000000. but this may require additional case handling when making calculations with negative numbers. so, you should decide which is more important to you. speed or accuracy? |
| |
JackAsser
Registered: Jun 2002 Posts: 2014 |
More than often I wish to represent numbers between 0 and 1 inclusive. Anybody got a nice solution to this. Currently I use 8.8-fixpoint, but it feels such a waste using only bit 0 in the upper 8 bits ONLY when it's the number 1. And using more exotic setups like 1.7-fixpoint is not so nice either I think, resulting in some shifts when you need the data. |
| |
Frantic
Registered: Mar 2003 Posts: 1648 |
JA: I am not sure why 1.7 fixpoint must necessarily result in shifts? Why not simply treat bit7 as 1? If I do not misunderstand things, if you really need to shift it so that you have a bit0 that stands for the "1" then this is the same as saying that you need to have the 1 in bit0 anyway which probably implies 1.8 fixpoint? Anyway.. please elaborate on your problem. I am sure you could solve it better than me anyway, but it would be interesting to hear. |
| |
Perplex
Registered: Feb 2009 Posts: 255 |
Quoting JackAsserMore than often I wish to represent numbers between 0 and 1 inclusive. Anybody got a nice solution to this.
One byte = 0/255 .. 255/255?
|
| |
WVL
Registered: Mar 2002 Posts: 902 |
Quote: Quoting JackAsserMore than often I wish to represent numbers between 0 and 1 inclusive. Anybody got a nice solution to this.
One byte = 0/255 .. 255/255?
that's even more nasty... |
| |
Perplex
Registered: Feb 2009 Posts: 255 |
Quoting WVLthat's even more nasty...
That really depends on what you're using them for, doesn't it? |