Re: [Flashforth-devel] Floating point math for FF 5.x - IEEE-754 format
Brought to you by:
oh2aun
From: om1zz <om...@vo...> - 2015-05-16 10:48:47
|
This is a short intro how the single precision (32bit) floating point numbers work in the lib: For example 3.141592653589793 converts into IEEE-754 single precision (32bit) floating point as: 0x40490FDA as not rounded, or 0x40490FDB as rounded. That converts back to decimal format as: 0x40490FDA = 3.1415925 not rounded 0x40490FDB = 3.1415927 rounded. So 32bit FP works with 7 decimal digits after the decimal point, 8 digits of mantissa (significand) in total are max what the IEEE-754 processes. For an input the library can process 1 dec place of mantissa more (as the mantissa still fits into signed long int during processing the decimal string), therefore you may enter f# 3.14159265e ( 314.159.265 fits ) and it will round properly into the binary FP format stored. Entering additional digits of mantissa may not fit into signed long int (used during processing of the string) so it throws an overrun. When printing with fs. (and the "precision" set to 7 - the number of ALL digits of mantissa to be printed out) you get f# 3.14159265e fs. 3.141593E0 ok When you want enter FP constants directly, you have to convert the decimal number into IEEE-754 single precision binary format and use (for example) as follows: $0fda $4049 fconstant fpi $0fda $c049 fconstant f-pi $0fda $3fc9 fconstant fpi/2 $0fda $bfc9 fconstant f-pi/2 Mind the sequence - lower word first, then the higher word. Or you may calculate directly with floats in hexa $0fda $3fc9 $0fda $bfc9 f* ( fpi/2 * f-pi/2 ) f# 3.14159265e $0fda $3fc9 f/ ( fpi / fpi/2 ) Happy floating, I. |