Pure Programmer
Blue Matrix


Cluster Map

Project: Amortization Table (Revisited)

In the AmortizationTable1 project we had a problem with accumulation error in our balance. We can avoid this problem by working with integers whenever possible. If we take value like the payment or balance that is represented in dollars and cents then multiply it by 100, we can convert a decimal value to an integer value. Re-write the Amortization Table project to use long integers for the payment, interest, principle and balance.

Output
$ rustc AmortizationTable2.rs error: unknown format trait `s` --> AmortizationTable2.rs:11:38 | 11 | ... println!("{}", format!("Syntax: {0:s} loan_amount rate_per_annum term_in_years", utils::program_name().expect("program name should ... | ^ | = 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` --> AmortizationTable2.rs:23:40 | 23 | println!("{}", format!("0\t\t\t\t{0:.2f}", (balance) as f64 / 100.0f64)); | ^ | = 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` --> AmortizationTable2.rs:35:31 | 35 | ... println!("{}", format!("{0:d}\t{1:.2f}\t{2:.2f}\t{3:.2f}\t{4:.2f}", i + 1, (payment) as f64 / 100.0f64, (principle) as f64 / 100.0f... | ^ | = 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` --> AmortizationTable2.rs:35:40 | 35 | ... println!("{}", format!("{0:d}\t{1:.2f}\t{2:.2f}\t{3:.2f}\t{4:.2f}", i + 1, (payment) as f64 / 100.0f64, (principle) as f64 / 100.0f... | ^ | = 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` --> AmortizationTable2.rs:35:49 | 35 | ... println!("{}", format!("{0:d}\t{1:.2f}\t{2:.2f}\t{3:.2f}\t{4:.2f}", i + 1, (payment) as f64 / 100.0f64, (principle) as f64 / 100.0f... | ^ | = 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` --> AmortizationTable2.rs:35:58 | 35 | ... println!("{}", format!("{0:d}\t{1:.2f}\t{2:.2f}\t{3:.2f}\t{4:.2f}", i + 1, (payment) as f64 / 100.0f64, (principle) as f64 / 100.0f... | ^ | = 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` --> AmortizationTable2.rs:35:67 | 35 | ... println!("{}", format!("{0:d}\t{1:.2f}\t{2:.2f}\t{3:.2f}\t{4:.2f}", i + 1, (payment) as f64 / 100.0f64, (principle) as f64 / 100.0f... | ^ | = 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 `args` in this scope --> AmortizationTable2.rs:10:5 | 10 | if args.len() != 4 { | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0425]: cannot find function `exit` in this scope --> AmortizationTable2.rs:12:3 | 12 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::process::exit; | error[E0425]: cannot find value `Utils` in this scope --> AmortizationTable2.rs:15:24 | 15 | let mut balance:i64 = Utils.stoiWithDefault(args[1], 0) * 100; | ^^^^^ not found in this scope error[E0425]: cannot find value `args` in this scope --> AmortizationTable2.rs:15:46 | 15 | let mut balance:i64 = Utils.stoiWithDefault(args[1], 0) * 100; | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0425]: cannot find value `Utils` in this scope --> AmortizationTable2.rs:16:29 | 16 | let mut interestRate:f64 = Utils.stodWithDefault(args[2], 0.05f64); | ^^^^^ not found in this scope error[E0425]: cannot find value `args` in this scope --> AmortizationTable2.rs:16:51 | 16 | let mut interestRate:f64 = Utils.stodWithDefault(args[2], 0.05f64); | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0425]: cannot find value `Utils` in this scope --> AmortizationTable2.rs:18:26 | 18 | let mut periods:isize = Utils.stoiWithDefault(args[3], 30); | ^^^^^ not found in this scope error[E0425]: cannot find value `args` in this scope --> AmortizationTable2.rs:18:48 | 18 | let mut periods:isize = Utils.stoiWithDefault(args[3], 30); | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0425]: cannot find function `pow` in this scope --> AmortizationTable2.rs:20:60 | 20 | let mut payment:i64 = (interestRate * balance / (1.0f64 - pow(1.0f64 + interestRate,-periods)) + 0.5f64) as i64; | ^^^ not found in this scope error: aborting due to 16 previous errors For more information about this error, try `rustc --explain E0425`.

Solution