Shop OBEX P1 Docs P2 Docs Learn Events
Another Rookie Question - Display a float !? — Parallax Forums

Another Rookie Question - Display a float !?

nestor73nestor73 Posts: 33
edited 2011-06-23 08:16 in Propeller 1
Hi all,

before i'm pulling out my hair, i'm going to try and explain my problem here. I have a simple
number that needs te be show on my VGA display. It's a float value (in order 1,5..8,5) but
i cannot seem to find an easy way to display it correctly. Why is that ??
       TurnsH:=(running_time/3)
       TurnsHF:=(running_time//3)
       tmp.displayString(strings.integertodecimal(TurnsH,2), tmp#white, tmp#blue, 20, 27 )
       tmp.displayString(strings.integertodecimal(TurnsHF,1), tmp#white, tmp#blue, 20, 31 )
       tmp.displayString(string(","), tmp#white, tmp#blue, 20, 30 )          

Running_time is a normal integer..
This does NOT give a correct result !

Comments

  • AleAle Posts: 2,363
    edited 2011-06-23 07:23
    Maybe you should use one of the Float objects from the OBEX. The propeller does not support floating point in hardware and that is why you have to use software emulation :)
  • nestor73nestor73 Posts: 33
    edited 2011-06-23 07:30
    Hi,

    guess i've tried all that there is to find in the OBEX. It just blows me away that an easy task like this (get a string out of a float) does not
    work properly...
    Ive also used the float_string , float_math, simple_numbers functions all without succes
  • Mike GreenMike Green Posts: 23,101
    edited 2011-06-23 07:46
    The FloatString object works fine for displaying floating point values and, by default, uses FloatMath to do its internal floating point manipulation. If it doesn't work for you, you'll have to provide the program you wrote for us to comment on. There are some simple examples of the use of FloatString and FloatMath included with the objects. Do remember that the built-in operators, like +, -, *, / only work for floating point constant values. They don't work for variables. They'll just treat the floating point value as an arbitrary long and do the integer operation on the internal representation of the floating point value ... not what you want or expect.
  • kwinnkwinn Posts: 8,697
    edited 2011-06-23 07:51
    Nestor73, you need to use the "FloatString" object, and most likely the "FloatToString(Single)" function.
  • nestor73nestor73 Posts: 33
    edited 2011-06-23 07:57
    Ok Mike , this explanation helped me allot. I am using FloatMath now to do the calculations,
    and FloatString to display them. That works really well now ! Thanx !
  • nestor73nestor73 Posts: 33
    edited 2011-06-23 07:58
    Yes kwinn, but the problem was that it alone displayed wrong things, i needed to use the floatmath to do single divisions etc..
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-06-23 08:16
    You could try the cfloatstr.spin object in CLIB. It contains two routines that convert a floating point number to a string. They are putfloate(str, x, width, digits) and putfloatf(str, x, width, digits). putfloate converts the number in "x" to a string in scientific notation and puts in it the buffer pointed to by "str". putfloatf uses standard notation. The parameter "width" specifies the minimum width of the output string, and it will pad with leading blanks if needed. "digits" specifies the number of fractional digits to generate. The buffer pointed to by "ptr" must be large enough to hold the generated string, which could be fairly long if you call putfloatf with the maximum floating point value.

    cfloatstr.spin is attached below. Note, there is line containing "repeat -exp10" that doesn't compile under older versions of BST. You can change this to "repeat 0-exp10" to get it to compile, or use the latest version of BST. Also, cfloatstr.spin doesn't require any additional floating point objects if you are only using it to print, and are not actually doing any floating point math.
Sign In or Register to comment.