Pure Programmer
Blue Matrix


Cluster Map

Project: Significant Digits

Write a function that rounds a floating point value to a specified number of significant digits. Significant digits are the the number of digits (omiting any leading zeros) that are actually known in a measured value. Use the math constants for π and the natural logarithm base, 𝑒, for your tests. Loop from 1 to 16 significant digits testing each value. Assert a precondition on the function to validate the proper range of 1-16 on the significant digits argument.

The roundSigDig() function should take the floating point value to round and the number of significant digits to keep (precision). It should return a new floating point value rounded to the number of significant digits. For example 1234 rounded to two significant digits is 1200 This rounding can be accomplished by multiplying the value (n) by 10int(precision-log10(n)-1), adding 0.5, computing the integer floor, then dividing by 10int(precision-log10(n)-1) again.

See [[Significant Figures]]

Output
$ rustc SignificantDigits.rs error: unknown format trait `d` --> SignificantDigits.rs:21:36 | 21 | println!("{}", format!("π to {0:d} significant digits: {1:.16f}", sd, roundSigDig(PI, sd))); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `f` --> SignificantDigits.rs:21:65 | 21 | println!("{}", format!("π to {0:d} significant digits: {1:.16f}", sd, roundSigDig(PI, sd))); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `d` --> SignificantDigits.rs:30:36 | 30 | println!("{}", format!("ℯ to {0:d} significant digits: {1:.16f}", sd, roundSigDig(E, sd))); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `f` --> SignificantDigits.rs:30:65 | 30 | println!("{}", format!("ℯ to {0:d} significant digits: {1:.16f}", sd, roundSigDig(E, sd))); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error[E0425]: cannot find value `PI` in this scope --> SignificantDigits.rs:21:86 | 21 | println!("{}", format!("π to {0:d} significant digits: {1:.16f}", sd, roundSigDig(PI, sd))); | ^^ not found in this scope | help: consider importing one of these items | 8 + use consts::PI; | 8 + use std::f32::consts::PI; | error[E0425]: cannot find value `E` in this scope --> SignificantDigits.rs:30:86 | 30 | println!("{}", format!("ℯ to {0:d} significant digits: {1:.16f}", sd, roundSigDig(E, sd))); | ^ not found in this scope | help: consider importing one of these items | 8 + use consts::E; | 8 + use std::f32::consts::E; | warning: unused import: `std::f64::consts` --> SignificantDigits.rs:8:5 | 8 | use std::f64::consts; | ^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default error[E0425]: cannot find function `log10` in this scope --> SignificantDigits.rs:12:35 | 12 | let p:isize = sigdigits - (floor(log10(value))) as isize - 1; | ^^^^^ not found in this scope | help: use the `.` operator to call the method `log10` on `f64` | 12 - let p:isize = sigdigits - (floor(log10(value))) as isize - 1; 12 + let p:isize = sigdigits - (floor(value.log10())) as isize - 1; | error[E0425]: cannot find function `floor` in this scope --> SignificantDigits.rs:12:29 | 12 | let p:isize = sigdigits - (floor(log10(value))) as isize - 1; | ^^^^^ not found in this scope | help: use the `.` operator to call the method `floor` on `f64` | 12 - let p:isize = sigdigits - (floor(log10(value))) as isize - 1; 12 + let p:isize = sigdigits - (log10(value).floor() as isize - 1; | error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields --> SignificantDigits.rs:13:30 | 13 | return floor(value * pow(10.f64,(p) as f64) + 0.5f64) / pow(10.f64,(p) as f64); | ^^^ | help: if intended to be a floating point literal, consider adding a `0` after the period | 13 | return floor(value * pow(10.0f64,(p) as f64) + 0.5f64) / pow(10.f64,(p) as f64); | + error[E0425]: cannot find function `pow` in this scope --> SignificantDigits.rs:13:23 | 13 | return floor(value * pow(10.f64,(p) as f64) + 0.5f64) / pow(10.f64,(p) as f64); | ^^^ not found in this scope error[E0425]: cannot find function `floor` in this scope --> SignificantDigits.rs:13:9 | 13 | return floor(value * pow(10.f64,(p) as f64) + 0.5f64) / pow(10.f64,(p) as f64); | ^^^^^ not found in this scope | help: use the `.` operator to call the method `floor` on `f64` | 13 | return (value * pow(10.f64,(p) as f64) + 0.5f64).floor() / pow(10.f64,(p) as f64); | ~ ~~~~~~~~~ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields --> SignificantDigits.rs:13:65 | 13 | return floor(value * pow(10.f64,(p) as f64) + 0.5f64) / pow(10.f64,(p) as f64); | ^^^ | help: if intended to be a floating point literal, consider adding a `0` after the period | 13 | return floor(value * pow(10.f64,(p) as f64) + 0.5f64) / pow(10.0f64,(p) as f64); | + error[E0425]: cannot find function `pow` in this scope --> SignificantDigits.rs:13:58 | 13 | return floor(value * pow(10.f64,(p) as f64) + 0.5f64) / pow(10.f64,(p) as f64); | ^^^ not found in this scope error: aborting due to 13 previous errors; 1 warning emitted Some errors have detailed explanations: E0425, E0610. For more information about an error, try `rustc --explain E0425`.

Solution