Shop OBEX P1 Docs P2 Docs Learn Events
Please translate into english thank you — Parallax Forums

Please translate into english thank you

pilot0315pilot0315 Posts: 913
edited 2022-04-06 06:16 in Propeller 2
PUB FSqr(singleA) : single | s, x, m, root

''Compute square root of singleA

  if singleA > 0                'if a =< 0, result 0

    Unpack(@s, singleA)         'unpack input

    m >>= !x & 1                'if exponent even, shift mantissa down
    x ~>= 1                     'get root exponent

    root := $4000_0000          'compute square root of mantissa
    repeat 31
      result |= root
      if result ** result > m
        result ^= root
      root >>= 1
    m := result >> 1

    return Pack(@s)             'pack result

ModEdit: code tags added

Comments

  • Are you aware how 32-bit floating point numbers are packaged (stored as bits)?
    The "Unpack" methods breaks the floating point number into three separate variables. "s" is the sign variable which indicates if the number is negative or not. "x" is the exponent of the floating point value and "m" is the mantissa of the floating point value.

    The method ends with a call to "Pack" which takes the three separate numbers (s, x and m) and packs the bits appropriately for a 32-bit float.

    The code in between "Unpack" and "Pack" is obviously some form of Voodoo enchantment.

    Floating point math is pain to use. You're almost always better off using integer math. As @Mickster said, "If you can't solve the problem with integers, you don't understand the problem." Take a look at this thread for a discussion about using integers instead of floats.

  • @Duane Degn, there may be enough wetware functioning tonight to believe the geographic origin of the likely sorcerer and original source of the noted incantation to be closer to Old World Druid magic than the Voodoo of the New World.

  • @"Duane Degn"
    @"frank freedman"

    Looks like: C12H17N2O4P to me. It makes my serotonin receptors freak out. I will look at the @Mickster thread. Thanks for the laugh and the assist.

  • evanhevanh Posts: 16,040
    edited 2022-04-07 04:39

    The loop is a bit-by-bit search of the exact square root.

        root := $4000_0000          ' begin from most significant positive bit
        repeat 31
          result |= root            ' set that bit in result
          if result ** result > m   ' compare input against result squared
            result ^= root          ' if result-squared is greater then remove that bit again
          root >>= 1                ' try next less significant bit
    
  • evanhevanh Posts: 16,040
    edited 2022-04-07 04:56

    Oh, and because it is for floating point, the used bits of the mantissa doesn't change. Which is why it can get away with just comparing the high 32 bits using ** operator.

  • pilot0315pilot0315 Posts: 913
    edited 2022-04-08 17:36

    @everybody

    I found this in the prop tool library: decfloat.spin2. Used the power functions and in stead of two squared is 4 using the power I get 8. What am I missing?
    obj

    ' main                                                          ' * master Spin cog
      term : "jm_fullduplexserial"
      number: "DecFloat"
    
    pub main ()
        x:=2
        y:=2
        z:=x*y
        n:= 2
    
        term.start(RX1, TX1, %0000, BR_TERM)
    
            waitms(10000)
         '  wait_for_terminal
            repeat
             term.fstr0(string("F_Pow(x, y)="))
             sqr:=number.  F_Pow(x, y)
    
             term.fxdec (sqr,1)
    
             term.tx(10)
    
             term.fstr0(string("z= x*y = ")) 
             term.dec( z)
              term.tx(9)
    
              term.fstr0(string(" F_PowInt(x, n)="))
             sqra:=number.   F_PowInt(x, n)
    
             term.dec (sqra)
    
             term.tx(13)
    

    Using the Jm_fullduplexserial to print somehow is giving me an issue with the floating points. Any examples of doing plain old math out there? The simple stuff sometimes is confusing.

    Thanks

  • evanhevanh Posts: 16,040

    FromInt(n) : converts signed integer to decimal float

  • You can't use integers as the input to most floating point methods. You first need to convert the number from an integer to a float.

    Enter a few numbers into this website and you can get an idea of how the floats are stored.

    The number 2 as an integer is binary %00000000000000000000000000000010. When 2 is stored as a 32-bit floating point number, the binary is %01000000000000000000000000000000. You can't perform integer math on floats and you can't perform floating point math with integers.

  • @evanh
    @"Duane Degn"
    @"frank freedman"

    Thanks I think I got it.
    Martin

Sign In or Register to comment.