Math.log10
func Math.log10(x: float64): float64Math.log10 returns the base-10 logarithm of x.
Details
Math.log10never panics.0.0gives-Infinity, a negativexgivesNaN,InfinitygivesInfinity, andNaNgivesNaN.
Basic examples
Math.log10(100.0) // 2.0
Math.log10(1000.0) // 3.0
Math.log10(2.0) // 0.3010299956639812Numbers between 0.0 and 1.0 have a negative logarithm:
Math.log10(0.01) // -2.0
Math.log10(0.5) // -0.3010299956639812Scope
1.0 gives 0.0, and both zeros give -Infinity:
Math.log10(1.0) // 0.0
Math.log10(0.0) // -Infinity
Math.log10(-0.0) // -InfinityA negative number has no real logarithm, and the infinities and NaN follow IEEE 754:
Math.log10(-1.0) // NaN
Math.log10(Math.Infinity) // Infinity
Math.log10(-Math.Infinity) // NaN
Math.log10(Math.NaN) // NaNThe result stays small across the whole float64 range, subnormal numbers included:
Math.log10(Math.maxFloat64) // 308.25471555991675
Math.log10(1e-300) // -300.0
Math.log10(5e-324) // -323.30621534311575An integer goes through float64 first:
let n = 1000000
Math.log10(float64(n)) // 6.0Pass it by name to Array.map, call it in a pipe, or name its parameter x:
[1.0, 10.0, 100.0].map(Math.log10) // [0.0, 1.0, 2.0]
1e6 | Math.log10() // 6.0
Math.log10(x: 1e6) // 6.0Applications
Express a power ratio in decibels:
func decibels(ratio: float64): float64 {
10.0 * Math.log10(ratio)
}
decibels(100.0) // 20.0
decibels(0.5) // -3.010299956639812Find the pH of a solution from its hydrogen-ion concentration:
let concentration = 1e-7
-Math.log10(concentration) // 7.0Find the order of magnitude of a number, such as the step for an axis tick:
func magnitude(x: float64): float64 {
Math.pow(10.0, Math.floor(Math.log10(x)))
}
magnitude(4321.0) // 1000.0
magnitude(0.05) // 0.01Compare earthquake magnitudes: each step on the scale is ten times the amplitude:
type Quake = { place: string, amplitude: float64 }
let quakes = [Quake{ place: "A", amplitude: 1000.0 }, Quake{ place: "B", amplitude: 100000.0 }]
for q in quakes {
println("${q.place}: ${Math.log10(q.amplitude)}")
}A: 3.0
B: 5.0Properties and relations
The logarithm of a product is the sum of the logarithms, and Math.pow(10.0, x) undoes it, each up to rounding. It is also Math.log(x) / Math.log(10.0):
func close(a: float64, b: float64): bool {
Math.abs(a - b) <= 1e-12 * Math.max(1.0, Math.max(Math.abs(a), Math.abs(b)))
}
for x in [0.25, 2.0, 7.0, 42.0, 1e10] {
assert(close(Math.log10(x * 3.0), Math.log10(x) + Math.log10(3.0)))
assert(close(Math.pow(10.0, Math.log10(x)), x))
assert(close(Math.log10(x), Math.log(x) / Math.log(10.0)))
}Math.log10 is increasing, so it keeps the order of positive numbers:
let xs = [0.001, 0.5, 1.0, 3.0, 1e9]
let ys = xs.map(Math.log10)
for i in Iter.range(1, ys.length()) {
assert(ys[i - 1] < ys[i])
}Possible issues
The result is a float64 and can land one digit below a whole number:
Math.log10(1e15) // 14.999999999999998
Math.log10(0.1) // -0.9999999999999999So counting digits with Math.floor(Math.log10(x)) + 1.0 can be off by one. For a non-negative int, count the characters of "${n}" instead:
let n = 1000000000000000
Math.floor(Math.log10(float64(n))) + 1.0 // 15.0
"${n}".length() // 16Math.log(x) / Math.log(10.0) is a different rounding of the same value, and can differ in the last digit:
Math.log10(1000.0) // 3.0
Math.log(1000.0) / Math.log(10.0) // 2.99999999999999960.0 gives -Infinity, which panics when converted to an integer:
int(Math.log10(0.0)) // panic: -Infinity does not convert to intAn integer argument is a compile error, since a numeric literal without a point is an int:
Math.log10(100) // error: cannot use int where float64 is expectedSee also
Math.log · Math.log2 · Math.exp · Math.pow · Math.floor · Math
