Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 15:39 25 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Numbers in MMBasic

Author Message
crackerjack

Senior Member

Joined: 11/07/2011
Location: Australia
Posts: 164
Posted: 12:27pm 10 Mar 2012
Copy link to clipboard 
Print this post

Hi all - is there anybody who can explain how floating point numbers are stored internally in MMBasic? As I understand it, a number is held in 4 bytes and can be split into these bytes by simply calling the I2C utility function num2byte.

From the manual:
The maximum number that can be represented is 3.40282347e+38 and the minimum is 1.17549435e-38

The following code snippets and results give a clue, but I haven't been able to unravel it as yet:

[code]
num2byte 1, a, b, c, d
[/code]

Results are:
a=0 &B00000000
b=0 &B00000000
c=128 &B10000000
d=63 &B00111111

While...
[code]
num2byte -1, a, b, c, d
[/code]

Results in:
a=0 &B00000000
b=0 &B00000000
c=128 &B10000000
d=191 &B10111111

The reverse function byte2num does as expected in recombining the bytes back into a number.

Any ideas?

Thanks - crackerjack.
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3800
Posted: 12:46pm 10 Mar 2012
Copy link to clipboard 
Print this post

Chances are it uses whatever PIC32 C does, which is probably what the chip itself does. And that's got a useful datasheet :)

Otherwise, PIC32 C is gcc (gnu cc) so consult its doc.

A lot of chips use IEEE754; this may well be one.

I really wouldn't want to be dealing with it the way you seem to be trying!! Maybe explain why you want to know...

JohnEdited by JohnS 2012-03-11
 
crackerjack

Senior Member

Joined: 11/07/2011
Location: Australia
Posts: 164
Posted: 01:17pm 10 Mar 2012
Copy link to clipboard 
Print this post

Hi John. Thanks for the reply. I guess my reason is to try understand what useful purpose the two I2C utility functions, byte2num and num2byte actually serve. That should probably have been the angle of my query. It would seem there must be some reason to want to split and/or recombine the bytes making up a floating point number, but looking at the complexity if IEEE754, I can't see why... Are there I2C devices that send/receive 32-bit floats as a stream of bytes?
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3194
Posted: 11:40pm 10 Mar 2012
Copy link to clipboard 
Print this post

Yes, MMBasic uses the single precision IEEE-754 floating-point format (http://en.wikipedia.org/wiki/IEEE_754-2008) which squeezes the number into one 32 bit word (called a float for short).

The byte2num and num2byte functions allow you to break this 32 bit word into individual 8 bit bytes. These functions were written by Gerard Sexton as part of the I2C library and I must admit that I don't know why they were needed (are you there Gerard?) I guess that you could use them to look inside a float number but there would be little point in doing that as MMBasic does a good enough job of manipulating floats in the first place.

If you are using floats to manipulate unsigned integers (whole numbers) it would be best to restrict the integers to 16 bits (ie, zero to 65536) as that leaves plenty of safety margin before you begin to loose accuracy in the float format. You could manipulate 32 bit numbers by using num2byte to get the individual bytes which you could then shift, increment, etc then reassemble into a float for storage using byte2num. The trouble is that this would be slow.

I hope that this helps and does not further confuse the situation.

Geoff
Geoff Graham - http://geoffg.net
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024