Forum Discussion
Best way of ensuring floating point precision?
I need to ensure I get floating point precision for division where the values I'm dividing could both be integers. The problem I'm seeing is if the two numbers are integers, the result isn't as accurate as I need it. I need at least precision to three decimal places. There are plenty of references to this in the expr man page and on DC (Click here). No one seems to have solid recommendations for forcing floating precision though.
There seem to be a few ways to do this. I'm wondering if anyone has experience in which would be the best. Here are a few options I've tested and the results:
Rule : This doesn't give floating point precision:
Rule : [expr {5 / 4}]: 1. Time: 17
Rule :
Rule : This gives the correct result and seems like the most thorough option (also the slowest):
Rule : [expr {[format %f 5] / [format %f 4]}]: 1.25. Time: 62
Rule :
Rule : This is tied for the fastest and seems like a valid approach:
Rule : [expr {[format %f 5] / 4}]: 1.25. Time: 42
Rule :
Rule : This is tied for the fastest but is a bit of a hack (by appending a . to one value before dividing):
Rule : [expr {5. / 4}]: 1.25. Time: 42
when RULE_INIT {
set iterations 10000
set a 5
set b 4
log local0. "==================================================================================== "
log local0. "[clock clicks -milliseconds]"
set start [clock clicks -milliseconds]
for {set i 0}{$i < $iterations}{incr i}{
set test [expr {$a / $b}]
}
log local0. "This doesn't give floating point precision:"
log local0. "\[expr {$a / $b}\]: [expr {$a / $b}]. Time: [expr {[clock clicks -milliseconds] - $start}]"
log local0. ""
set start [clock clicks -milliseconds]
for {set i 0}{$i < $iterations}{incr i}{
set test [expr {[format %f $a] / [format %f $b]}]
}
log local0. "This gives the correct result and seems like the most thorough option (also the slowest):"
log local0. "\[expr {\[format %f $a\] / \[format %f $b]}\]: [expr {[format %f $a] / [format %f $b]}].\
Time: [expr {[clock clicks -milliseconds] - $start}]"
log local0. ""
set start [clock clicks -milliseconds]
for {set i 0}{$i < $iterations}{incr i}{
set test [expr {[format %f $a] / $b}]
}
log local0. "This is tied for the fastest and seems like a valid approach:"
log local0. "\[expr {\[format %f $a\] / $b}\]: [expr {[format %f $a] / $b}]. Time: [expr {[clock clicks -milliseconds] - $start}]"
log local0. ""
set start [clock clicks -milliseconds]
append a .
for {set i 0}{$i < $iterations}{incr i}{
set test [expr {[format %f $a] / $b}]
}
log local0. "This is tied for the fastest but is a bit of a hack (by appending a . to one value before dividing):"
log local0. "\[expr {$a / $b}\]: [expr {$a / $b}]. Time: [expr {[clock clicks -milliseconds] - $start}]"
log local0. "[clock clicks -milliseconds]"
log local0. "===================================================================================="
}
Thanks,
Aaron
- spark_86682Historic F5 Account
I need to ensure I get floating point precision for division where the values I'm dividing could both be integers. The problem I'm seeing is if the two numbers are integers, the result isn't as accurate as I need it. I need at least precision to three decimal places.
Here are a few options I've tested and the results:
There are plenty of references to this in the expr man page and on DC (Click here). No one seems to have solid recommendations for forcing floating precision though.
- hoolio
Cirrostratus
Hi Spark,
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com