Python Forum
Round float according to uncertainty
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Round float according to uncertainty
#1
Hello everyone,

I'm familiar with the rounding function in python but at work I encountered a new situation.

Say you have a measurement result like so: (3.406 ± 0,052) mg/L.

There is a norm (DIN 1333) we use which states, that the result 3.406 mg/L has to be rounded to 3.41 mg/L according to its' uncertainty of 0.052 mg/L because the first digit behind the comma which differs from zero (in this case 5) occurs at the second decimal place.

To make matters more complicated a result like (9.4053 ± 0,021) mg/L has to be rounded to 9.405 mg/L because even though the uncertainty also "starts" at the second digit after the comma, the digit there is <3 and therefore the rounding place moves "one to the right".

Accordingly (1,024.45 ± 10.5 ) mg/L should be rounded to 1024 mg/L
and (1,024.45 ± 30.4 ) mg/L should be rounded to 1020 mg/L.

The latter (numbers bigger than zero) can be understood if you just write the values in exponential form like 10.2445 * 10^-2 and 0.105 * 10^-2.

How can I implement this in python without creating a barrage of if statements? In short: is there a simple rounding function which takes uncertainty into account?

Thanks in advance
Markus




In short
Reply
#2
you can use fixed point arithmetic, where you have full control over rounding
python.org has a section on this method here: https://docs.python.org/3/library/decimal.html

This, by the way, is the same method that we (Spectrochemical engineering Lab, Instrumentation Laboratories 1980's) used to do all of the math for a scanning plasma monochromator. Never used floating point. Programming language was forth.
Reply
#3
CatorCanulis Wrote:How can I implement this in python without creating a barrage of if statements?
Implement a working solution with a barrage of if statements, then we'll see if it is possible to write it another way.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020