el_supremo:
You still don't seem to have grasped the point that there are only about 6 or 7 digits of precision in a float.
-1.977905036 is ten digits of precision. Of those, only the first seven are significant in a 32-bit float and the rest are not stored. So your number is stored as approximately -1.977905 and when you print it, trying to print more than those seven digits is meaningless.
Pete
robtillaart:
for one decimal digit a binary representation needs 2log(10) bits ~ 3.322 bits
the mantisse in a IEE754 float is 23 bits.
That means the mantisse can hold max 23/3.322 ~ 6.924 decimal digits, all other are discarded.
(as in fact the first bit is always a 1 one could say 24/3.322 ~ 7.225 decimal digits)
1.977905036 has 10 digits so the float will represent the number nearest to 1.977905.
Then printing it again involves converting that binary representation to decimals again, meaning multiplications by 10 to split of digits (among other math). These multiplications introduce rounding errors of their own and will in the end generate "additional decimals"
"Floating point numbers are like piles of sand; every time you move one you lose a little sand and pick up a little dirt."
Thank guy guys, now I'm beging to understand where those last decimals come from, so basically it's a behavoiur of "casting" and "decasting" a number.
michinyon:
What is this piece of crap supposed to calculate ?
pow(10,-1) ????
Really ?
Well, -1.977905036*pow(10,-1) can be probably substitute by -0.1977905036 and will save us from calculate a silly operation.
michinyon:
Have you actually looked at the behavior of your ridiculous polynomial ?
yes, It's a nice curve, what's the problem?
michinyon:
What range of volts do you expect to be measuring ?
michinyon:
Do you have an A/D converter that is precise to at least 6 decimal digits over that voltage range ?
from 0 to 5v
michinyon:
Do know how the pow( ) function is implemented, or how accurate it is ?
no idea
robtillaart:
A float in a PC is most often a (64bit) double and internally in the processor it is even an 80 bit IIRC.
On a typical machine running Python, there are 53 bits of precision available for a Python float,
- 14. Floating Point Arithmetic: Issues and Limitations — Python 2.7.18 documentation -
enough said,
did you do the accuracy tests already?
Where did you get the formula from?
So, in python I don't have no problem because can store more decimals than in arduino right?
I have to test more to see if Horner solves my problem, hope this week get time.
zoomx:
DiSCLAiMER,
what sensor are you using?
Take in account that your Arduino A/D converter has only 1024 values from 0 to 1023.
The temperature sensor is an automotive fluid temperature sensor. The brand gives a table with the resistance the sensor has depending of the temperature exposed, so, as we can not measure resistance directly, I used a simple voltage divisor circuit to be able to measure resistance.
So the values of the table are interpolated in a grade 10 polynomial to convert voltage to temperature.
Yes, I know, so instead of using the arduino uno adc i'm using a 12bit adc mcp3008, so I have values from 0 to 4095.
And what about this
Python code:
adc=3669
volts=adc*(5.0/4095.0)
temp = -1.977905036pow(10,-1)pow(volts,10)+5.021263808pow(volts,9)-54.79044923pow(volts,8)+335.8665566pow(volts,7)-1268.136896pow(volts,6)+3037.582741pow(volts,5)-4566.827221pow(volts,4)+4061.676731pow(volts,3)-1782.286594pow(volts,2)+53.30656433*volts+342.0406525
print temp
output: 38.5857557064
Arduino code:
void setup() {
Serial.begin(9600);
}
void loop() {
int adc = 3669;
float volts = adc*(5.0/4095.0);
float temp = -1.977905036pow(10,-1)pow(volts,10)+5.021263808pow(volts,9)-54.79044923pow(volts,8)+335.8665566pow(volts,7)-1268.136896pow(volts,6)+3037.582741pow(volts,5)-4566.827221pow(volts,4)+4061.676731pow(volts,3)-1782.286594pow(volts,2)+53.30656433*volts+342.0406525;
Serial.println(temp);
}
output: 37.70