Shop OBEX P1 Docs P2 Docs Learn Events
Square root — Parallax Forums

Square root

Jonathan HolleJonathan Holle Posts: 48
edited 2010-08-01 18:54 in Propeller 1
How to calculate the square root of a variable without truncation and with floating point ?

Comments

  • TimmooreTimmoore Posts: 1,031
    edited 2010-07-31 19:17
    Theres several float objects that support sqr, they store a float in 32bits so its not without truncation, they store a float as 1bit sign, 7bits mantissa, 24bits exponent so you can work out the accuracy of the float, its called single precision IEEE-754.
  • Jonathan HolleJonathan Holle Posts: 48
    edited 2010-07-31 19:27
    Thanks a lot, it's perfect for what I want to do [noparse];)[/noparse]
  • Jonathan HolleJonathan Holle Posts: 48
    edited 2010-07-31 19:48
    And How can I debug the result in order to see if it works correctly ?
  • TimmooreTimmoore Posts: 1,031
    edited 2010-07-31 20:19
    there is an object called FloatString that converts a float to a string for display
  • Jonathan HolleJonathan Holle Posts: 48
    edited 2010-07-31 20:40
    Can't do it correctly :/

    here is my code :

    OBJ

    debug : "Fullduplexserial"
    f : "Float32Full"
    fstr : "floatstring"

    ...

    Pub Main | a, x, y
    Debug.start(31, 30, 0, 57600)
    f.start

    pxa3 := fstr.floattoformat(f.cos(30), 5, 3)

    DEBUG.str(string("test :"))
    DEBUG.str(pxa3)


    It debug 1.00 ... shocked.gif
  • TimmooreTimmoore Posts: 1,031
    edited 2010-07-31 21:53
    pxa3 := fstr.floattoformat(f.cos(30.0), 5, 3)
    

    To put a constant in float format you need a . i.e. 30 is 30.0 otherwise you can use f.ffloat to convert e.g

    pxa3 := fstr.floattoformat(f.cos(f.FFloat(30)), 5, 3)
    

    is the equivalent
    ·
  • Jonathan HolleJonathan Holle Posts: 48
    edited 2010-07-31 22:46
    it works perfectly !! thank you for your help [noparse];)[/noparse]
  • Jonathan HolleJonathan Holle Posts: 48
    edited 2010-08-01 08:00
    Arght, Don't works with a variable ... jumpin.gif why ?
  • TimmooreTimmoore Posts: 1,031
    edited 2010-08-01 08:05
    Is the variable an integer or float, if its an integer then f.ffloat is needed to convert to a float before using any other float operations on it.
  • Jonathan HolleJonathan Holle Posts: 48
    edited 2010-08-01 08:08
    it's a float sad.gif
  • Jonathan HolleJonathan Holle Posts: 48
    edited 2010-08-01 08:23
    The result I got is not correct :

    pxa3 := fstr.floattoformat(f.cos(40.0), 6, 3)
    DEBUG.str(string("test :"))
    DEBUG.str(pxa3)

    I got pxa3 = -0,667
  • John AbshierJohn Abshier Posts: 1,116
    edited 2010-08-01 13:03
    Did you actually mean 40.0 radians or did you want 40.0 degrees? The argument to cos is in radians. The cos of 40.0 radians on my calculator is -.667

    John Abshier
  • Jonathan HolleJonathan Holle Posts: 48
    edited 2010-08-01 18:54
    I did't know that it was in radian :-D, wonderful, works well ... thanks
Sign In or Register to comment.