flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > [fasmg] display 1.33e7

Author
Thread Post new topic Reply to topic
_shura



Joined: 22 May 2015
Posts: 61
_shura 20 Sep 2017, 12:43
Ohai,
is there a simple way to display floating point values in fasmg or do I have parse IEEE 751 manually?
Code:
Repeat 1, temp: ( 1.33e7 )
  display `temp
End Repeat
    

Unfortunately does not work.
Post 20 Sep 2017, 12:43
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
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.
Post 20 Sep 2017, 13:18
View user's profile Send private message Visit poster's website Reply with quote
_shura



Joined: 22 May 2015
Posts: 61
_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.
Post 20 Sep 2017, 15:03
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
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    
It may need more work, but I do not have too much time on my hands at the moment.
Post 20 Sep 2017, 17:13
View user's profile Send private message Visit poster's website Reply with quote
_shura



Joined: 22 May 2015
Posts: 61
_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
Post 20 Sep 2017, 17:56
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 20 Sep 2017, 18:38
See my last sentence above.
Post 20 Sep 2017, 18:38
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.