flat assembler
Message board for the users of flat assembler.
Index
> Macroinstructions > [fasmg] display 1.33e7 |
Author |
|
Tomasz Grysztar 20 Sep 2017, 13:18
Instead of parsing the output format, you can extract the fields of a floating point value with BSR, SHL and TRUNC operators.
There is also a simple showfloat macro that you could use as a starting point, but it does not generate scientific notation. |
|||
20 Sep 2017, 13:18 |
|
_shura 20 Sep 2017, 15:03
I solved it, but I do not really like this solution:
Code: null = 0 Struc float2decimal value*, precision: ( 4 ) Local absValue, signValue, absPower, signPower, absReal If ( value = 0.0 ) absValue = ( 0.0 ) Virtual At null dq value Load temp qword From null End Virtual If ( temp and 0x8000000000000000 ) signValue Equ '-' Else signValue Equ '+' End If Else If ( value < 0.0 ) absValue = ( 0 - value ) signValue Equ '-' Else absValue = ( 0 + value ) signValue Equ '+' End If absPower = ( 0 ) If ( absValue = 0.0 ) signPower Equ '' Else If ( absValue >= 10.0 ) While ( absValue >= 10.0 ) absValue = ( absValue / 10.0 ) absPower = ( absPower + 1 ) End While signPower Equ '+' Else If ( absValue < 1.0 ) While ( absValue < 1.0 ) absValue = ( absValue * 10.0 ) absPower = ( absPower + 1 ) End While signPower Equ '-' Else signPower Equ '' End If If ( precision > 0 ) Virtual At null db '.' rb precision db '' temp = ( absValue - ( trunc absValue )) Repeat ( precision ), offs: ( 1 ) temp = ( temp * 10.0 ) Store byte ( '0' + ( trunc temp )) At offs temp = ( temp - ( trunc temp )) End Repeat If ( temp >= 0.499999999999999999 ) Repeat ( precision ), offs: ( null ) Load temp byte From ( precision - offs ) If ( temp = '9' ) temp = '0' Else temp = temp + 1 End If Store byte temp At ( precision - offs ) If ( temp <> '0' ) Break End If End Repeat If ( temp = '0' ) absValue = ( absValue + 1 ) End If End If Load absReal ( precision + 1 ) From null End Virtual Else If ( precision > 0 ) err 'float2decimal', 'precision must be a positive integer or null' End If Repeat 1, integerPart: ( trunc absValue ) Repeat 1, powerPart: ( absPower ) . Equ signValue, `integerPart, string absReal, 'e', signPower, `powerPart End Repeat End Repeat End Struc temp float2decimal -1.337e-23 display temp, 10 Its very tricky to round the values correctly, because 0.5 is in fact 0.4999… -0.0 is in fact different from +0.0, but fasmg says, that they are equal, so I had to get the sign from the binary value, where it is encoded correctly. |
|||
20 Sep 2017, 15:03 |
|
Tomasz Grysztar 20 Sep 2017, 17:13
This is a loop-less method I experimented with:
Code: LG2 = 0.30102999566398119521373889472449302676818988146210854131 struc showfloat value,precision:2 local x,pow,t,sgn,txt x = value if x = 0 . = '0' else sgn = 0 if x < 0 x = -x sgn = 1 end if pow = trunc(bsr x * LG2) if pow-precision >= 0 repeat 1, p:pow-precision x = x / 1e#p end repeat else repeat 1, p:-pow+precision x = x / 1e-p end repeat end if if x < 1e#precision x = x * 10 pow = pow - 1 end if t = trunc x if x - t >= float 1/2 t = t + 1 end if if t >= 10e#precision x = x / 10 pow = pow + 1 t = trunc x if x - t >= float 1/2 t = t + 1 end if end if if pow>=0 repeat 1, p:pow txt = 'e' + `p shl 8 end repeat else repeat 1, p:-pow txt = 'e-' + `p shl 16 end repeat end if repeat 1, i:t txt = `i + txt shl (8*lengthof `i) end repeat txt = txt and 0FFh + '.' shl 8 + (txt shr 8) shl 16 if sgn txt = txt shl 8 + '-' end if . = string txt end if end struc tmp showfloat -1.337e-23 display tmp |
|||
20 Sep 2017, 17:13 |
|
_shura 20 Sep 2017, 17:56
But this failes with
Code: temp showfloat -13.3775e-23, 4 display temp ;=> -1.3377e-22 and not as expected -1.3378e-22 |
|||
20 Sep 2017, 17:56 |
|
Tomasz Grysztar 20 Sep 2017, 18:38
See my last sentence above.
|
|||
20 Sep 2017, 18:38 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.