Pure Programmer
Blue Matrix


Cluster Map

Project: Compute π

Write a program that computes and prints the value π which can be easily computed with standard math functions using one of the formula:

  • atan(1.0)*4.0
  • atan2(0.0, -1.0)
  • acos(-1.0)
  • 2.0*acos(0.0)

Which formula produces an answer closer to the standard math definition of π?

See [[Pi]]

Output
$ rustc ComputePi1.rs error: unknown format trait `f` --> ComputePi1.rs:15:37 | 15 | println!("{}", format!("pi1: {0:.15f}", pi1)); | ^ | = 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` --> ComputePi1.rs:17:37 | 17 | println!("{}", format!("pi2: {0:.15f}", pi2)); | ^ | = 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` --> ComputePi1.rs:19:37 | 19 | println!("{}", format!("pi3: {0:.15f}", pi3)); | ^ | = 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` --> ComputePi1.rs:21:37 | 21 | println!("{}", format!("pi4: {0:.15f}", pi4)); | ^ | = 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 --> ComputePi1.rs:16:62 | 16 | println!("{}", format!("abs(pi1 - π): {0:.10e}", fabs(pi1 - PI))); | ^^ not found in this scope | help: consider importing one of these items | 7 + use consts::PI; | 7 + use std::f32::consts::PI; | error[E0425]: cannot find value `PI` in this scope --> ComputePi1.rs:18:62 | 18 | println!("{}", format!("abs(pi2 - π): {0:.10e}", fabs(pi2 - PI))); | ^^ not found in this scope | help: consider importing one of these items | 7 + use consts::PI; | 7 + use std::f32::consts::PI; | error[E0425]: cannot find value `PI` in this scope --> ComputePi1.rs:20:62 | 20 | println!("{}", format!("abs(pi3 - π): {0:.10e}", fabs(pi3 - PI))); | ^^ not found in this scope | help: consider importing one of these items | 7 + use consts::PI; | 7 + use std::f32::consts::PI; | error[E0425]: cannot find value `PI` in this scope --> ComputePi1.rs:22:62 | 22 | println!("{}", format!("abs(pi4 - π): {0:.10e}", fabs(pi4 - PI))); | ^^ not found in this scope | help: consider importing one of these items | 7 + use consts::PI; | 7 + use std::f32::consts::PI; | warning: unused import: `std::f64::consts` --> ComputePi1.rs:7:5 | 7 | use std::f64::consts; | ^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default error[E0425]: cannot find function `atan` in this scope --> ComputePi1.rs:10:16 | 10 | let pi1:f64 = atan(1.0f64) * 4.0f64; | ^^^^ not found in this scope | help: use the `.` operator to call the method `atan` on `f64` | 10 - let pi1:f64 = atan(1.0f64) * 4.0f64; 10 + let pi1:f64 = 1.0f64.atan() * 4.0f64; | error[E0425]: cannot find function `atan2` in this scope --> ComputePi1.rs:11:16 | 11 | let pi2:f64 = atan2(0.0f64,-1.0f64); | ^^^^^ not found in this scope | help: use the `.` operator to call the method `atan2` on `f64` | 11 - let pi2:f64 = atan2(0.0f64,-1.0f64); 11 + let pi2:f64 = 0.0f64.atan2(-1.0f64); | error[E0425]: cannot find function `acos` in this scope --> ComputePi1.rs:12:16 | 12 | let pi3:f64 = acos(-1.0f64); | ^^^^ not found in this scope | help: use the `.` operator to call the method `acos` on `f64` | 12 | let pi3:f64 = (-1.0f64).acos(); | ~ ~~~~~~~~ error[E0425]: cannot find function `acos` in this scope --> ComputePi1.rs:13:25 | 13 | let pi4:f64 = 2.0f64 * acos(0.0f64); | ^^^^ not found in this scope | help: use the `.` operator to call the method `acos` on `f64` | 13 - let pi4:f64 = 2.0f64 * acos(0.0f64); 13 + let pi4:f64 = 2.0f64 * 0.0f64.acos(); | error[E0425]: cannot find function `fabs` in this scope --> ComputePi1.rs:16:51 | 16 | println!("{}", format!("abs(pi1 - π): {0:.10e}", fabs(pi1 - PI))); | ^^^^ not found in this scope error[E0425]: cannot find function `fabs` in this scope --> ComputePi1.rs:18:51 | 18 | println!("{}", format!("abs(pi2 - π): {0:.10e}", fabs(pi2 - PI))); | ^^^^ not found in this scope error[E0425]: cannot find function `fabs` in this scope --> ComputePi1.rs:20:51 | 20 | println!("{}", format!("abs(pi3 - π): {0:.10e}", fabs(pi3 - PI))); | ^^^^ not found in this scope error[E0425]: cannot find function `fabs` in this scope --> ComputePi1.rs:22:51 | 22 | println!("{}", format!("abs(pi4 - π): {0:.10e}", fabs(pi4 - PI))); | ^^^^ not found in this scope error: aborting due to 16 previous errors; 1 warning emitted For more information about this error, try `rustc --explain E0425`.

Solution