Shop OBEX P1 Docs P2 Docs Learn Events
FloatMath.spin Problem — Parallax Forums

FloatMath.spin Problem

crgwbrcrgwbr Posts: 614
edited 2007-01-23 23:20 in Propeller 1
I'm using the FloatMath object to to multiply a variable (Set_Depth) by 108.··Here is the·line of code:

Set_Depth := FloatMath.FMul(Set_Depth,108)

The variable Set_Depth is entered by a user earlier on.· It should just take Set_Depth and multiply it by 108, then put the answer back in Set_Depth.· For some reason though, it's just putting 0 in Set_Depth; no matter what the user entered.· I've been working on the problem for·5 hours; I can't figure out what's going on.· Anyone have an Idea?

Thanks,
crgwbr

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
NerdMaster
For
Life

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-01-23 19:45
    You have to put a decimal point on the "108". FMul multiplies two floating point numbers and the compiler thinks you wanted an integer since it doesn't know how you're going to use it. Try "FMul(Set_Depth,108.0)"
  • crgwbrcrgwbr Posts: 614
    edited 2007-01-23 19:52
    I set Set_Depth to 5.000 and the other factor to 108.000. Instead of returning 540, it returned 5000. Strange.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    NerdMaster
    For
    Life
  • Mike GreenMike Green Posts: 23,101
    edited 2007-01-23 20:10
    How are you displaying the result? You should be using FloatString. Show the whole program or the relevant section of it. There may be something else you're doing.
  • crgwbrcrgwbr Posts: 614
    edited 2007-01-23 20:25
    This time I used FloatString. It returned 7.567e-40. This works out to 0.0000000000000000000000000000000000000007567. Not exactly what it should be. I'm am displing the data on the Debug screen of the Basic Stamp Editor. Here is the relivent code:

    PUB Get_Depth

    Serial.Str(string("!NB0R02"))
    Set_Depth := Serial.rxDec
    Set_Depth := FM.FMul(Set_Depth,108.000)
    Depth := FS.FloatToString(Set_Depth)

    Serial[noparse][[/noparse]0].Str(string("Display Started, Depth Returned from Pink: "))
    Serial[noparse][[/noparse]0].Str(Depth)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    NerdMaster
    For
    Life

  • Mike GreenMike Green Posts: 23,101
    edited 2007-01-23 21:22
    Ah! You also forgot to convert the result from Serial.rxDec to floating point. Keep in mind that the compiler knows very little about floating point. In particular, there are no floating point variables, only integer ones. As far as the compiler is concerned, FMul takes two integer parameters and produces an integer result. It's just that the floating point routines interpret the integers as floating point values.

    Anyway, just use "Set_Depth := FM.FFloat(Serial.rxDec)". I think that'll help.
  • crgwbrcrgwbr Posts: 614
    edited 2007-01-23 22:26
    Well, it's definatly getting closer. Now it's outputing 540000 instead of 540.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    NerdMaster
    For
    Life
  • Mike GreenMike Green Posts: 23,101
    edited 2007-01-23 22:37
    I don't know what you're actually getting in from the serial receiver, but the rxDec routine is an integer routine. It doesn't know about decimal points. It looks like your input number is actually 5000, not 5. If it always has 3 decimal digits, you can just divide (FDiv) by 1000.0 to scale it.
  • crgwbrcrgwbr Posts: 614
    edited 2007-01-23 23:20
    Thanks Mike, that did it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    NerdMaster
    For
    Life
Sign In or Register to comment.