Unable to get floating point number (decimal value) from integer
Nicopowers
Posts: 12
in Propeller 1
Hello everybody my name is Nicolas, I am extremely new to Spin language. I already setup a temperature sensor but I want to get decimal values from it. I do not know how to setup the floating point libraries, I have searched in the internet and OBEX and i know that i have to use FloatMath, Float32, Float32Full but I can not get any of them to work. A simple code like the following:
I would expect to get a 1.0 in the parallax serial terminal, but i always get 106535321. I am reading a temperature sensor from an MCP3208, and I want to convert the voltage level to a temperature value. So i need to calculate the following formula:
voltage level = (data * 3.3) / (1023)
And i want to be able to get decimal values I have tried the following code:
And all i get is random big numbers, can somebody please help me? I've fallen and i cant get up!
CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 OBJ pst : "Parallax Serial Terminal" fl : "Float32" PUB Main fl.start pst.start(57600) pst.Str(String(pst#cs, pst#NL, pst#HM, "my float number is ")) pst.dec(fl.FFloat(1))
I would expect to get a 1.0 in the parallax serial terminal, but i always get 106535321. I am reading a temperature sensor from an MCP3208, and I want to convert the voltage level to a temperature value. So i need to calculate the following formula:
voltage level = (data * 3.3) / (1023)
And i want to be able to get decimal values I have tried the following code:
pst.Str(String(pst#NL, "temperature= ")) temp1 := adc.in(2) pst.dec(f32.FFloat((temp1* 3.3)/1023))
And all i get is random big numbers, can somebody please help me? I've fallen and i cant get up!
Comments
Also, you can't use arithmetic operators (like +, -, *, and /) on floats - they only work on integers. You must use f32.fadd, f32.fmul, etc. instead.
For example, the last line of the second piece of code you wrote should be: Note the 1023.0 to tell the compiler it should be a float and not an integer.
That could be further simplified to the following, which saves a runtime divide:
I'm not sure if you really need the constant() in there, or if the 1023 inside the constant() needs the decimal point - I never really understood the rules for floating point constants. Floating point math with arithmetic operators works at compile time, but at runtime you need to use f32 methods.
-Phil
If you can't do that inside constant(), there's some place inside of CON blocks where you can.
Just try it and see what works.
Multiply the temperature reading from the MCP by 3226 (( 3.3 / 1023 ) x 1,000,000 )
Convert the result to decimal and place the decimal point where needed.
To place a decimal point within the number in the correct location (as suggested by kwinn) I use a method I call "DecPoint". Here's a link to this method.
I added the simple "TtaMethodSigned" method to make Tracy Allen's "TtaMethod" a bit more useful. If you know the three numbers you're using are all positive, you can skip the "TtaMethodSigned" method and simply call the "TtaMethod."