Shop OBEX P1 Docs P2 Docs Learn Events
Newbie Programming Questions — Parallax Forums

Newbie Programming Questions

roboticsrobotics Posts: 90
edited 2010-05-05 08:19 in Propeller 1
Hi,

<i hope this didn't get posted multiple times as I had to press the "submit" button multiple times>

A have a couple super basic Spin programming questions, regarding the following code (below):

I noted that when reading from the DAT area either numeric constants (pi) or square roots of constants (^^2), or the products of two constants (2.0 * 3.0). the correct resultant numbers (3.1416, 1.1412, 6) are displayed in Examples 1,2,3. I also noted that in Example 4 that when the numeric floating point constant "9.0" is used as an argument in the display command, that it also displays properly as "9".

Two questions:
1) Why is it that in Example 5 when the variable "Temper" is used to store the product of "9.0 * 3.0", and then this variable is used as the argument for the display function that the number "0" is displayed instead of "27"?

2) In Example 6, why does the argument "9.0 * 3.0" yield "0" whereas when two operands and a multiplication operator are retrieved from the DAT area and used in the display argument that the correct result is displayed?

In advance, many thanks for your assistance!

CON

_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000


OBJ

term : "tv_terminal"
fp : "FloatString"


PUB start | i, Temp , Temper

'start the tv terminal
term.start(12)

'change color
term.out(3)

fp.SetPrecision(5)

' EXAMPLE 1 Displays "3.1416"
Temp := fnums1[noparse][[/noparse]0]
term.str(fp.FloatToString(Temp))
term.out(13)

' EXAMPLE 2 Displays "1.4142"
Temp := fnums1
term.str(fp.FloatToString(Temp))
term.out(13)

' EXAMPLE 3 Displays "6"
Temp := fnums1
term.str(fp.FloatToString(Temp))
term.out(13)
term.out(13)

' EXAMPLE 4 Displays "9"
term.str(fp.FloatToString(9.0))
term.out(13)

' EXAMPLE 5 Displays "0" ????????
Temper := 9.0 * 3.0
term.str(fp.FloatToString(Temper))
term.out(13)

' EXAMPLE 6 Displays "0" ?????????
term.str(fp.FloatToString(9.0 * 3.0))
term.out(13)

DAT

fnums1 long pi, ^^2.0, 2.0 * 3.0, 3.0 * 7.0, 1.0 / ^^2.0, -0.0000000000194, 1.0 / 13.0, 0

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2010-05-05 02:34
    SPIN doesn't do run-time float arithmetic (you'd need objects like Float32 to do that). In your examples (5, 6) you end up with decimal 27 ($0000001B) which is then interpreted as a floating point number which in turn comes out as 0 due to being the wrong format (floating point 27.0 would be $41D80000).
  • roboticsrobotics Posts: 90
    edited 2010-05-05 07:49
    Hi,

    Thank you for your explanation. Just one follow-up question. Why did the floating point constant expression "2.0 * 3.0" in the DAT section get properly evaluated at run time whereby the displayed result was "6"?

    Again my thanks as I was now able to write a program using a floating point object!
  • kuronekokuroneko Posts: 3,623
    edited 2010-05-05 07:55
    robotics said...
    Why did the floating point constant expression "2.0 * 3.0" in the DAT section get properly evaluated at run time whereby the displayed result was "6"?
    DAT sections are done at compile time. If you look at the hex display after compiling (F8) you'll find the binary representation for 6.0 in there. No trace of 2.0 * 3.0 [noparse]:)[/noparse]
  • roboticsrobotics Posts: 90
    edited 2010-05-05 08:12
    Hi,

    I hope I don't sound like that old tv show Columbo, but if you don't mind, if I could ask one last question.

    I commented out the float object and ran the program and noted that as you said the "2.0 * 3.0" was performed at compile time. But, if the propeller doesn't perform float operations without a float object, how did the propeller compile the float expression of "2.0 * 3.0" ?

    In advance, thank you.
  • kuronekokuroneko Posts: 3,623
    edited 2010-05-05 08:19
    robotics said...
    I hope I don't sound like that old tv show Columbo, but if you don't mind, if I could ask one last question.
    One of my favourites ...
    You said...
    But, if the propeller doesn't perform float operations without a float object, how did the propeller compile the float expression of "2.0 * 3.0" ?
    It's not the propeller doing the compilation. It's all done on your PC (running the Propeller Tool or bst[noparse][[/noparse]c]). SPIN and PASM have to be compiled/assembled into a propeller binary which is then subsequently uploaded to the propeller chip.
Sign In or Register to comment.