Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / is it a bug or am I missing something ?

Author
Message
Twinsen
18
Years of Service
User Offline
Joined: 20th Jun 2006
Location: Romania
Posted: 1st Apr 2008 12:06 Edited at: 1st Apr 2008 12:28
here's my simple testing code:



as you can see, I expect the last value displayed to be 255 ... if you run the code, everything goes well until value 57 ... after that, it seems that the values are getting a small amount in extra, and I don't understand WHY ... it all seems so messy right now ... please help me understand

Could you help me treat my injured Dino-Fly ?
Veron
17
Years of Service
User Offline
Joined: 22nd Nov 2006
Location:
Posted: 1st Apr 2008 12:56
Weird, it worked perfectly for me, it continued at the same pace when it reached 57.


Twinsen
18
Years of Service
User Offline
Joined: 20th Jun 2006
Location: Romania
Posted: 1st Apr 2008 13:09 Edited at: 1st Apr 2008 13:11
are u serious ??? was the last value 255 ??? cause for me, when it reaches 57 it starts giving values like 57.60001 .......... 58.12001 ........... 58.18001 etc. ... in the end, the last value is 254.49001 or something like that ( I don't remember the exact digits )

Could you help me treat my injured Dino-Fly ?
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 1st Apr 2008 13:25 Edited at: 1st Apr 2008 13:30
twinsen,

to say it simply, "it is the way of things".

damn, i wish i could remember where i heard that line. i use it too much to not know where i got it from

here's an old thread on the subject:

http://forum.thegamecreators.com/?m=forum_view&t=68022&b=15

do a forum search for "decimal places" and you'll see everyone was equally as surprised as you the first time they realized "it" existed. also, in that thread is mention of some ways to work around it (UDT's, parsing your variables for more accurate "accuracy", etc).

Virtual Nomad
AMD XP 1800+ (~1.6 Ghz) / 1.5 GB RAM
ATI Radeon 8700LE 128 MB / Windows XP
Twinsen
18
Years of Service
User Offline
Joined: 20th Jun 2006
Location: Romania
Posted: 1st Apr 2008 13:48
what ??? this is more than plain STUPID ... I don't get it ... how the hell did computers come to exist in the first place with such mathematical errors ??? man I feel my whole computer is an error ... for real now, I don't care about the power of 2 problem ... this is just a simple matter of storing a number ... also, if a#=5.2 is being returned as 5.19999980927, this has nothing to do with any computation thing ... it's like the computer has bad eye sight or hearing problems or WHATEVER ... I just don't get it ...

Could you help me treat my injured Dino-Fly ?
HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 1st Apr 2008 14:37
well if you do it as a double float you don't get the odd numbers at 57.



Dark Physics makes any hot drink go cold.
pcRaider
17
Years of Service
User Offline
Joined: 30th May 2007
Location:
Posted: 1st Apr 2008 16:33
How in this?

Twinsen
18
Years of Service
User Offline
Joined: 20th Jun 2006
Location: Romania
Posted: 1st Apr 2008 19:05
hmm that seems to work ... but can someone please explain to me what's the difference between real, float and double float types ??? thanks

Could you help me treat my injured Dino-Fly ?
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 1st Apr 2008 20:14 Edited at: 1st Apr 2008 20:15
real = float
double = double the storage of a float and double the accuracy.

Quote: "this is more than plain STUPID"

It is absolutely the opposite.
4 bytes can hold a total of around 4 billion numbers in integer form.
Floats use the same amount of storage but can be used to represent numbers from as small as 1.2 x 10^-38 (ie, move the decimal place left 38 places), and as large as 3.4 x 10^38 (ie, move the decimal place right 38 places). The cost is some inaccuracy on a logarithmic scale (the larger the number gets, the more inaccurate it is). However, there are STILL only 4 billion possible numbers in a float.

If you keep in mind that floats are always approximate, you won't be too surprised.

Twinsen
18
Years of Service
User Offline
Joined: 20th Jun 2006
Location: Romania
Posted: 1st Apr 2008 20:17
hmm so does the computer, in general, when working with bigger and bigger numbers, approximates the approximation of the last approximation made, and so on ??? cause this is the only way I find to give such errors ... cause otherwise (normal cases), it would be logical for a value to be more accurate when having more decimals ...

Could you help me treat my injured Dino-Fly ?
jinzai
18
Years of Service
User Offline
Joined: 19th Aug 2006
Location: USA
Posted: 1st Apr 2008 21:53 Edited at: 1st Apr 2008 21:54
Hang on a second, and reread IanM's post.

Computers don't have decimal places. (Decimal places are base-10 inventions, and computers work only in binary...base 2.) Not hex, or octal, or decimal, but binary. Any other numeric representation is software generated.

I think that when you say 'having more decimals', you are thinking that the computer gets more to work with...it doesn't. It still has 32-bits to work with, and must shoehorn all possible (infinite) values into that container.

http://www.engr.udayton.edu/faculty/jloomis/ece314/notes/fpu/fpu.html
Twinsen
18
Years of Service
User Offline
Joined: 20th Jun 2006
Location: Romania
Posted: 1st Apr 2008 22:10
your explanation was clear, concise and very helpful ... but that link you gave me is for a cyborg mind, not me I cannot yet comprehend all the mathematics involved in the instruction set of the CPU's core ... anyway, so what you're trying to say is that errors occur because the computer tries to convert all decimal, hex, etc. numbers to actual binar data ???

Could you help me treat my injured Dino-Fly ?
jinzai
18
Years of Service
User Offline
Joined: 19th Aug 2006
Location: USA
Posted: 2nd Apr 2008 00:49 Edited at: 2nd Apr 2008 00:52
Yes, it was a little much. The last time, I posted a Wiki that was only slightly less daunting. Frankly, it gives me a headache, too.

I really didn't quite get it myself until I started working in data acquisition, and had to figure out A/D converters.

I am saying that, but its really the other way around. Computers convert binary to the other formats. Its only in error when dealing with non-integers. The reason is that there are an infinite number of numbers between 1.0 and 2.0, for example.

Computer memory is just a series of on/off switches, the value of the switch increases by a power of two with each successive bit. Hexadecimal is used alot because it takes 4 bits to represent the 16 values that a single digit hex number can take. (0xF - 15), and that is called a nibble. (Its half a byte)

2^0 = 1 = 0x01 = 0000 0001
2^1 = 2 = 0x02 = 0000 0010
2^2 = 4 = 0x04 = 0000 0100
2^3 = 8 = 0x08 = 0000 1000
2^4 = 16 = 0x10 = 0001 0000
2^5 = 32 = 0x20 = 0010 0000
2^6 = 64 = 0x40 = 0100 0000
2^7 = 128 = 0x80 = 1000 0000
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 2nd Apr 2008 04:46 Edited at: 2nd Apr 2008 04:55
twinsen,

here's something else to consider regarding this matter:


look at the str$ values on the left compared to the float values on the right to see what's going on.

point being there are relatively simple ways to achieve what you're looking for. and, i hear you... who'd have thought we had to write a function to do the simplest math?

edit: version 2, conversion back to float included... don't want to steer you the wrong way (re-conversion = ## on the right).


Virtual Nomad
AMD XP 1800+ (~1.6 Ghz) / 1.5 GB RAM
ATI Radeon 8700LE 128 MB / Windows XP
Twinsen
18
Years of Service
User Offline
Joined: 20th Jun 2006
Location: Romania
Posted: 2nd Apr 2008 07:28
wow !!! thanks a lot guys !!! you've helped me a lot ... but I still don't get something. I know you're gonna shoot me but please help me understand it 100% so, why is it that in my code, the error appears only after value 57 and then it disappears after some numbers ???

Could you help me treat my injured Dino-Fly ?
jinzai
18
Years of Service
User Offline
Joined: 19th Aug 2006
Location: USA
Posted: 2nd Apr 2008 14:56
I was hoping that someone else would have a good answer for that, but...the error is not consistent, nor scalar. There is also alot of information regarding calculating it, but...since I'd be using a computer to calculate it, it seems to be to be a rather fruitless pursuit.

Here is the Wiki page, the part about rounding explains it somewhat, and there are some informative links at the bottom of the page. The calculators provide a nice way to look at the actual values used internally for real numbers.
http://en.wikipedia.org/wiki/IEEE_754
Twinsen
18
Years of Service
User Offline
Joined: 20th Jun 2006
Location: Romania
Posted: 2nd Apr 2008 16:52
hmm but why do other languages have this fixed ??? even Turbo Pascal does it right ... trust me, I've checked it out today

Could you help me treat my injured Dino-Fly ?
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 2nd Apr 2008 19:38
They don't.

Any language that uses the floating-point instructions built into the processor will have this problem - they may disguise it when outputting or be using doubles to reduce the problem (which you can do too), but they have it nonetheless.

Any language that doesn't have the problem, and is not disguising it, is using an emulated floating-point library or a bignum library as an alternative, won't be limiting itself to 4 bytes of storage, and will run many times slower instead.

Try this in turbo pascal - Put the number 2147483000 into an longint, then assign that number to a single (not a real), and then assign that single back to another longint. Print all 3 values.

Do the same in any other language you want.

For example, in C this is:


Compiling and running this using GCC, I get:
2147483000
2147483008.000000
2147483008

Now I don't think that the most widely used compiler in the world on the most widely used processor in the world has a bug and that no-one has spotted it for the 10 or 11 years I've been using the many versions of this compiler, so it must be normal. If it's normal for GCC, then it's probably normal for other compilers too ... including DBPro.

So, how about just accepting that floating-point has limitations, and that there's nothing that can be done about it.

... trust me, I know what I'm talking about.

Twinsen
18
Years of Service
User Offline
Joined: 20th Jun 2006
Location: Romania
Posted: 2nd Apr 2008 21:41
lol I wouldn't dare argue your knowledge you and TDK are true gold mines around here all I was saying is that it seems weird for a float to assign those +/- 38 digits to all numbers even when it's not the case ... In my code, why does the error appear if the numbers only have one decimal ??? numbers are 5.x ...... 57.x .......... 123.x ....... 233.x and so on ... so the program actually uses more than it's required ... just my thoughts anyway

Could you help me treat my injured Dino-Fly ?
Twinsen
18
Years of Service
User Offline
Joined: 20th Jun 2006
Location: Romania
Posted: 3rd Apr 2008 16:45 Edited at: 3rd Apr 2008 16:47
ok ... I got that but this is BEYOND my comprehension : just have a look for yourselves



if you can convince me that I'm the mad one and the computer is working properly, I'll take a therapy session, I promise !!! I mean I got the fact that somewhere in the memory, the computer is trying to approximate numbers who's decimals "stretch out" into the infinite and for example 5.2 might get 5.2000000118 or something like that, but 1.99 becoming 1 is just too much

Could you help me treat my injured Dino-Fly ?
pcRaider
17
Years of Service
User Offline
Joined: 30th May 2007
Location:
Posted: 3rd Apr 2008 17:48 Edited at: 3rd Apr 2008 17:49
Twinsen
18
Years of Service
User Offline
Joined: 20th Jun 2006
Location: Romania
Posted: 3rd Apr 2008 17:55
and the explanation is ???

Could you help me treat my injured Dino-Fly ?
pcRaider
17
Years of Service
User Offline
Joined: 30th May 2007
Location:
Posted: 3rd Apr 2008 18:33
Explanation is very simple.
Lee decided the rule.
Obey a rule of Lee.

There is FPSC.
Lee made FPSC.
There is DBpro for FPSC.
Only for a game,
DBpro was made.

As for DBpro, calculation precision is bad,
However, processing speed is fast.
We use fast DBpro of speed.
Our purpose is game making.
Twinsen
18
Years of Service
User Offline
Joined: 20th Jun 2006
Location: Romania
Posted: 3rd Apr 2008 18:36
and how can I make a game when I stumble at every step because in the world of computers, it seems now that 1.99 is the same as 1 ... excuse me but I don't see the logic in Lee's rule ... anyway where's the rule written ??? for such paradoxal errors, a huge watermark should be made, saying "BEWARE OF THE FLOATTT !!!!" ... sorry for the sarcasm but it's like finding out your mom ain't who you thought she was for all your life ...

Could you help me treat my injured Dino-Fly ?
pcRaider
17
Years of Service
User Offline
Joined: 30th May 2007
Location:
Posted: 3rd Apr 2008 18:41
Our purpose is game making.

There is a highly precise calculation such as an electronic calculator,
However, as for it, computing speed is very slow.
It is computing speed that is necessary for our game.

Our purpose is game making.
Twinsen
18
Years of Service
User Offline
Joined: 20th Jun 2006
Location: Romania
Posted: 3rd Apr 2008 18:57
damn you sound like an elite Cyborg ... can anyone (human) please explain this to me ??? also,

if purpose = gamemaking then provide_working_tools()

damn, you say u wanna make games but how can I do that if I want to calculate the sinus of 199/100 ??? cause you do realise a collision system would go WRONG if 1.99 equals 1 ...

Could you help me treat my injured Dino-Fly ?
pcRaider
17
Years of Service
User Offline
Joined: 30th May 2007
Location:
Posted: 3rd Apr 2008 19:05 Edited at: 3rd Apr 2008 19:07
100 <> 100.0

This is not equal.
An integer is different from a floating point.

Can this understand it?
Twinsen
18
Years of Service
User Offline
Joined: 20th Jun 2006
Location: Romania
Posted: 3rd Apr 2008 19:23
what ??? I mean I know that the variable type differs, but the values are the same, right ??? even in math, 3 is natural, integer, rational, real and complex AND it's the same in all cases ... why is 100 as integer <> than 100.0 as real ???

Could you help me treat my injured Dino-Fly ?
hyrichter
20
Years of Service
User Offline
Joined: 15th Feb 2004
Location: Arizona
Posted: 3rd Apr 2008 19:41
What you're doing in your example of 199/100 is dividing 2 integers. The compiler assumes you want an integer back for an answer. If you put a decimal point on either one, it realizes that you're talking about a float. It's just one of the things to realize with DBP.

CodeSurge
Version 1.0 finally released! Code your DBP projects in style. (And save the kittens!)
pcRaider
17
Years of Service
User Offline
Joined: 30th May 2007
Location:
Posted: 3rd Apr 2008 19:44 Edited at: 3rd Apr 2008 19:46
` integer = integer/integer

print 8/3
print 8 mod 3
print
wait key

print 8.0/3.0
wait key

print (3*2)+2
wait key

There are eight people and divides it by 3.
Two people remain by a calculation.

http://en.wikipedia.org/wiki/Division_%28mathematics%29
pcRaider
17
Years of Service
User Offline
Joined: 30th May 2007
Location:
Posted: 3rd Apr 2008 19:51 Edited at: 3rd Apr 2008 19:58
Delete
Twinsen
18
Years of Service
User Offline
Joined: 20th Jun 2006
Location: Romania
Posted: 3rd Apr 2008 20:06 Edited at: 3rd Apr 2008 20:07
wow !!! the creator of CodeSurge helps me and he does a great job !!! thanks man, this was a very helpful and clear, short answer ... I was surprised cause other languages did not experience these kinds of problems ... also, why isn't this explained somewhere ???

@pcRaider: are you using a translator ??? your posts are ... how should I put it ... weird and mechanical ... no offense

Could you help me treat my injured Dino-Fly ?
pcRaider
17
Years of Service
User Offline
Joined: 30th May 2007
Location:
Posted: 3rd Apr 2008 20:45
I'm sorry in bad English.
DBP has much BUG.
want to a bug-fix.
Twinsen
18
Years of Service
User Offline
Joined: 20th Jun 2006
Location: Romania
Posted: 3rd Apr 2008 21:32 Edited at: 3rd Apr 2008 21:33
yeah I know, DBPro has many bugs ... why should 100<>100.0 if I already specify the # symbol at the end of the variable ... so the program should expect I want a real number ... I mean from what I see (pay attention to what I say now), it seems that a simple operation like 199/100 is automatically rounded or having the INT() command applied ... this is WRONG !!!

Could you help me treat my injured Dino-Fly ?
H4ck1d
18
Years of Service
User Offline
Joined: 27th Dec 2005
Location: Yes
Posted: 3rd Apr 2008 21:39
Not really - you gave it two integers, why should dbpro assume that you wanted a float answer?

jinzai
18
Years of Service
User Offline
Joined: 19th Aug 2006
Location: USA
Posted: 3rd Apr 2008 22:19 Edited at: 3rd Apr 2008 22:20
Finally, H4ck1d said it [again]. 199.0 / 100.0
Twinsen
18
Years of Service
User Offline
Joined: 20th Jun 2006
Location: Romania
Posted: 3rd Apr 2008 22:37
thanks a lot WindowsKiller !!! I wanna ask u something though:

1) what do you mean the code is being parsed from R to L ???
2) if instead of 199/100 I have 199.0/100 or 199/100.0, how is this treated ??? I mean I see it returns a float but is it sufficient for just one value to be float if I want a float answer ? thanks

Could you help me treat my injured Dino-Fly ?
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 3rd Apr 2008 22:59
Yes, just one needs to be a float. DBPro converts to float on demand, for example:

First, (integer 5) divided by (integer 2) gives a result of (integer 2). This multiplied by (float 3.0) will cause the (integer 2) to be converted to a float before being multiplied. The result is (float 6), not the (float 7.5) you would expect from a purely floating point calculation.

Again though, this is the way it's done in many other languages too, including C and C++. It's not wrong, it's not right, it's just the way it is, mostly for what were originally performance reasons - integer calculations were faster than floating point calculations. It's not really that way any more, but you can't just change the rules halfway through the game.

Twinsen
18
Years of Service
User Offline
Joined: 20th Jun 2006
Location: Romania
Posted: 3rd Apr 2008 23:14 Edited at: 3rd Apr 2008 23:17
yeah I get it now but think of this: I program for 5 years and I've been making games under a dozen loads of engines and languages, etc. ... and here I am, after 5 years of programming, having almost 30 games completed, other 20 abandoned or on hold, and I feel like an idiotic baby, finding out that all this time I've been messing around with something I thought I knew, but I didn't knew AT ALL .... I think tutorials ( in every other language like C++, Delphi, etc. ) should focus more on this type of problems rather than making a newbie code his own "Hello World" program ... it's plain stupid ... the smart guys make the beginners feel like "ohh it's so easy to code ... I just type in s*** and programs pop out" while instead, they should focus on the underneath mechanics of the computer ... and tell them how the input is handled, about the keyboard buffer ( another stupid problem I confronted some time ago ), about the system internal clock, the CPU ticks, the internal step of the program which is constant for every PC, this variable issue, the carriage symbols being returned ( my READLN problem under Pascal ) and many others ... don't you agree ??? it's not funny seeing tutorials like "making your first spinning cube program" while you don't even understand what happens on the inside ... just my thoughts

Could you help me treat my injured Dino-Fly ?
jinzai
18
Years of Service
User Offline
Joined: 19th Aug 2006
Location: USA
Posted: 3rd Apr 2008 23:35
I would absolutely agree with that. It is more likely, however that the smart guys have either not realized this either (Likely in most cases.), or they are simply writing to the standard so that the code is portable enough to be educational.

For the record, there is an 80 bit FP format, I don't recall the IEEE spec, but it is called a 10BYTE in ASM, iirc. You can use 10BYTES in PCs, but you need the FP and emulator libraries to do so, and they are of course slow, and well...10 bytes each. I had the amazing library Spontaneous Assembly (1991) which had a nice Quad Word FP library, which implemented floats in assembly, with or without FPU support.
Twinsen
18
Years of Service
User Offline
Joined: 20th Jun 2006
Location: Romania
Posted: 4th Apr 2008 07:26
wow ! now that's cool

Could you help me treat my injured Dino-Fly ?
Twinsen
18
Years of Service
User Offline
Joined: 20th Jun 2006
Location: Romania
Posted: 4th Apr 2008 14:03 Edited at: 4th Apr 2008 14:10
ahhh so first the calculation is done and THEN assigned to the variable ... THANKS A LOT ... another very important thing I didn't know hmm but all these problems made me forget: what is the simple and rational explanation for the INITIAL problem??? the one where after value 57, you could see a small error as in 57 was 57.0000001 etc. ??? I mean the past few days I've been reading studies on numerology and computer fractions, etc. but what is the exact cause to my problem because I am not forcing the computer to work with huge decimals ... I only instruct it to add 0.6 for several times so I don't see the problem ...

Could you help me treat my injured Dino-Fly ?
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 4th Apr 2008 21:43 Edited at: 4th Apr 2008 21:44
Binary fractions is probably the bit you are missing. Let's see if I can explain.

You most likely know integer binary – you start with 1, the value to its left is double that (2) and the value to the left is double that (4) and so on.

Now imagine that your number has a binary point (a binary version of the decimal point).

Again starting with 1, the value to its right is half that (0.5) and the value to the right of that is half again (0.25) and so on (0.125) and so on (0.0625)...

Now imagine that you only have 24 bits to store your number (0.6) in, and that the binary point is at the far left.

Start like so:
24 - 0.5
23 - 0.25
22 - 0.125
21 - 0.0625
20 - 0.03125
19 - 0.015625
18 - 0.0078125
17 - 0.00390625
16 - 0.001953125
15 - 0.0009765625
14 - 0.00048828125
13 - 0.000244140625
12 - 0.0001220703125
11 - 0.00006103515625
10 - 0.000030517578125
09 - 0.0000152587890625
08 - 0.00000762939453125
07 - 0.000003814697265625
06 - 0.0000019073486328125
05 - 0.00000095367431640625
04 - 0.000000476837158203125
03 - 0.0000002384185791015625
02 - 0.00000011920928955078125
01 - 0.000000059604644775390625

Now the job is to use these numbers to get 0.6 by either adding or ignoring each number as you go along. Every time you add a number write down a 1 and every time you ignore a number, write down a 0.

0.5 + 0.0625 + 0.03125 + 0.00390625 + 0.001953125 + 0.000244140625 + 0.0001220703125 + 0.0000152587890625 + 0.00000762939453125 + 0.00000095367431640625 + 0.000000476837158203125 + 0.000000059604644775390625 ... and then we run out, with a total value of 0.599999964237213134765625. We couldn't get to 0.6 with the level of precision available to us.

Now let's map that back to your issue:
- Your chosen number was 0.6, so I used that too
- The number 24 is one greater than the number of bits available within the floating point format for the fractional part of the representation. In IEEE754 format (used by Intel processors), bit 24 is always assumed to be set, so doesn't need to be stored and therefore using 23 bits of the storage, not 24.
- One bit is used for the sign (1 = negative).
- 8 bits are left over to represent how much and which way to shift the binary point. For 0.6, no shift is required.

Does that help explain? If needed I can provide you with an OpenOffice spreadsheet that will help you understand how numbers from 0.5 to 1 are represented, or with a little more work on it, I think I can get it to handle any number within the normal float range.

Twinsen
18
Years of Service
User Offline
Joined: 20th Jun 2006
Location: Romania
Posted: 4th Apr 2008 22:03
wow !!! now that WAS VERY helpful !!! I've also spent 3 hours learning all the binary things like the ones' complement, the twos' complement, how to calculate fractional binary numbers, how to convert them to different bases, learnt the difference between the + and - zero (0), learnt almost all operations with binaries, learnt about the sign and magnitude of a binary, etc. and I must say I was pretty stupid yesterday man your post was AWESOME !!! just one last question: did you do all that math by yourself ??? I especially want to know how did you manage to find out what numbers was 0.6 composed of ( the part where you found the right combinations of decimals ) ... you ROCK

Could you help me treat my injured Dino-Fly ?
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 4th Apr 2008 22:22
Windows calculator (which uses a slow big-num library, but speed doesn't matter here and accuracy does), and notepad.

Start with '1', divide by 2 24 times and copy the numbers to notepad - that got the list.

Next, start with your number of 0.6, and then using the list, work down the list subtracting numbers, making sure to never allow the number to go negative.

I'm just glad that you chose a number between 0.5 and 1.0 so I didn't need to explain the shifting of the binary point

Twinsen
18
Years of Service
User Offline
Joined: 20th Jun 2006
Location: Romania
Posted: 4th Apr 2008 22:36
ah which reminds me (sorry) you said there are 8 remaining bits for the integer part of the number ... just 2 quick questions:

1) if I have 8 bits for the integer part, 23 for the fractional part and 1 for the sign, is this why 1+23+8=32 as in a 32-bit Windows or any other OS ???
2) if I have to shift the binary point for let's say 23.4 ... how do I do that ? just tell me in 1-2 lines, no need to go through all that trouble again

Could you help me treat my injured Dino-Fly ?
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 4th Apr 2008 23:23
1) It's 32 bits for the lower-precision floating point format, yes, but some OS's may differ.

2) Don't know if I can do it in one or two lines, but here goes ...

Remember that the 'invisible' bit 24 is always set to 1? Well the FP format also places the binary point immediately after this as the 'zero-offset', meaning that a decimal value of 1 will have an offset of 0, bit 24 set, then the binary point, and finally be followed by 23 zero bits. If you have a decimal value of 2, the offset will be 1, moving the binary point one place right. If you have a decimal value of 0.5, the offset will be -1, moving the binary point one place left.

Now just to complicate things a little, the offset is itself adjusted too (the technical term is that it has a bias). To get the actual offset stored in the float, you take the offset and add 127 to it. The range of valid values then runs from 254 for the largest numbers down to 1 for the smallest numbers.

These format numbers are what are called the normalised numbers.

You might have spotted that there are 2 numbers missing from that final offset range - 0 and 255.

255 first - When the offset is 255 and the 23 other bits are all zero, the number represents either positive or negative infinity (because of the sign bit). If the 23 other bits are any other value than zero, they are treated as 'unknown' or NaN (not a number) - an invalid value. Any calculations that take place using these numbers effectively become the same number (ie, 2 * infinity = infinity, or 10 * NaN = NaN)

Now 0 - these numbers are what are called the denormalised numbers - you can pretty much ignore this number type, except for just 2 combinations of values. If the 23 other bits are all zero, then the value is treated as either positive or negative zero (remember that sign bit).

Twinsen
18
Years of Service
User Offline
Joined: 20th Jun 2006
Location: Romania
Posted: 4th Apr 2008 23:58
hmm I think I got it but it still bugs me out about something ... I know I'm giving u a hard time and I apologize but can you please make a FULL number example (draw or write it in the post, even highlighted if you want), showing me which is the sign bit, the invisible bit, the decimals, the fractional part, the integer, the binary point, etc ??? cause I think I misunderstood the position of the sign bit, invisible bit and binary point

Could you help me treat my injured Dino-Fly ?
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 5th Apr 2008 00:22
Here's code that will demonstrate the basics - it includes the official names for each of the sections of the float:


See if that helps - remember that negative exponents (the offset) shift the binary point to the left.

Try putting values of 0.25, 0.5, 1, 2 & 4 and see how it affects the exponent - you'll see it's a simple shift of the binary point, and directly equivalent to the way that decimal multiply's or divides by 10 to shift the decimal point.

Twinsen
18
Years of Service
User Offline
Joined: 20th Jun 2006
Location: Romania
Posted: 5th Apr 2008 00:29 Edited at: 5th Apr 2008 00:32
thanks !!! I will try it when I get home in the mean time, I found out that computer binary numbers differ from the one we learn in maths because the exponent is also biased ... I found this thing:

http://upload.wikimedia.org/wikipedia/commons/e/e8/IEEE_754_Single_Floating_Point_Format.svg

here I see that the exponent, though is stored as 124, is in fact 124-127 thus -3 and the true number is calculated my a forumula: number = sign (+/-1) * exponent (127-the binary number stored in the exponent) * fraction part (1.xxxxxxxxxxxxxxxxxxxxxxx where the "x" represents the stored bits inside teh fraction part) ... hmm I'm really bugged right now ... maybe 1-2 hours away from this will help me clear my head thanks a bunch and if you've coded that only for me, I'll shoot myself sorry for all this trouble

Could you help me treat my injured Dino-Fly ?

Login to post a reply

Server time is: 2024-09-27 12:15:28
Your offset time is: 2024-09-27 12:15:28