-
-
Notifications
You must be signed in to change notification settings - Fork 237
/
Approximate.scala
54 lines (47 loc) · 1.57 KB
/
Approximate.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// (C) Uri Wilensky. https://github.com/NetLogo/NetLogo
package org.nlogo.api
import annotation.strictfp
object Approximate {
@strictfp
def approximate(n: Double, places: Int): Double =
// the 17 here was not arrived at through any deep understanding of IEEE 754 or anything like
// that, but just by entering different expressions into NetLogo and noting that I couldn't seem
// to come up with an expression that would make more than 17 decimal places print; an example
// that makes 17 places print is "show 0.1 - 0.00000000000000001" -- I think there may still be
// theoretical correctness issues here, perhaps involving very large or very small numbers, but
// for now this'll have to do - ST 5/3/02
if (places >= 17)
n
else {
val multiplier = StrictMath.pow(10, places)
val result = StrictMath.floor(n * multiplier + 0.5) / multiplier
if (places > 0)
result
else
StrictMath.round(result)
}
@strictfp
def approximateCeiling(n: Double, places: Int): Double =
if (places >= 17)
n
else {
val multiplier = StrictMath.pow(10, places)
val result = StrictMath.ceil(n * multiplier) / multiplier
if (places > 0)
result
else
StrictMath.round(result)
}
@strictfp
def approximateFloor(n: Double, places: Int) =
if (places >= 17)
n
else {
val multiplier = StrictMath.pow(10, places)
val result = StrictMath.floor(n * multiplier) / multiplier
if (places > 0)
result
else
StrictMath.round(result)
}
}