Shop OBEX P1 Docs P2 Docs Learn Events
Float32 object question — Parallax Forums

Float32 object question

BitsBits Posts: 414
edited 2012-02-07 21:53 in Propeller 1
Is there a better way to do this? Or is there a feature in the object that will do this?

I want to use a floating point number but only with a precision of 2 decimal places.

using float32 object ...
  out := 23.3699

  out := Math.Fmul(out,100.0)
  out := Math.Fround(out)
  out := Math.Ftrunc(out)
  Out := Math.Fdiv(out,100.0)


Then out would be 23.36

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-02-07 14:34
    Ftrunc following Fround is redundant. You could do this:
    out := Math.Fdiv(Math.Fround(Math.Fmul(out, 100.0)),100.0)
    

    But be aware that floating point is done in binary, not decimal, and 0.37 has no exact representation. When you eventually print out your result, you may well get 23.369998 or somesuch, instead of 23.37.

    -Phil
  • Mike GreenMike Green Posts: 23,101
    edited 2012-02-07 14:44
    When you use FloatString to convert the floating point values for display purposes, you can specify the number of decimal places to be displayed with some of the routines and they will round the result to however many decimal places you request.
  • BitsBits Posts: 414
    edited 2012-02-07 15:38
    Thanks guys I am trying to buffer my ADC numbers before they are sent into a DAC. Its complicated so I have not the time to describe it here but, I think I got it.

    Again thanks.
  • MagIO2MagIO2 Posts: 2,243
    edited 2012-02-07 21:53
    I don't see a good reason to buffer ADC-values after conversion to float. Usually your ADC has some numbers of bits of resolution. Your DAC also has a certain resolution usually also related to some number of bits. Let's say your ADC has 16 bit and your DAC has 8 bit. Then you simply do a right shift by 8 bits and there is the value you have to send to the DAC which can be bufferend.

    If it is about displaying the numbers it is better to use fixed point arithmetics. The value can easily be calculated without float which is slower and which needs an object, so extra RAM.

    Rule of three is your friend here. Let's say your reference voltage of the ADC is 3.3V which means that you get an ADC value of $0000 for 0V and $ffff for 3.3V. You simply calculate
    330 * ADCvalue / $ffff. Now your result is in the range of 0 - 330. The last 2 digits are the fractional part of your number. When converting this number to a string you simply use a function which puts the point between digit 1 and 2 (counted starting with 0 and from right to left).

    If you want to round up at >=x.xx5 and down at <x.xx5 you simply add another digit to the result and add 5
    3300 * ADCvalue / $ffff + 5. Now you either use the same function as above and feed it with result/10 or you tweak the function to put the point between digit 2 and 3 and skipping digit 0 at all.
Sign In or Register to comment.