From 4fb5ff22ef573dda6538143505508c9605e9c6a5 Mon Sep 17 00:00:00 2001 From: martin machiebe Date: Fri, 17 Jan 2025 14:18:19 +0100 Subject: [PATCH 1/2] feat: implement operational functions --- src/main.rs | 3 ++ src/primitive_data_types.rs | 79 +++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 src/primitive_data_types.rs diff --git a/src/main.rs b/src/main.rs index 3cc3d3e..3feb341 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,8 @@ +mod primitive_data_types; + fn main() { intro_to_u(); + primitive_data_types::intro_to_us(); } diff --git a/src/primitive_data_types.rs b/src/primitive_data_types.rs new file mode 100644 index 0000000..03a4b9c --- /dev/null +++ b/src/primitive_data_types.rs @@ -0,0 +1,79 @@ +use core::convert::Into; + +pub fn intro_to_us() { + let result_sum: u16 = sum(10, 7).into(); + let result_substrt: u16 = substraction(23, 17); + let result_division: u16 = division(25, 5); + let result_multiply: u16 = multiply(23, 17); + let is_even: bool = is_even(22, 2); + let is_sum_even: bool = is_sum_even(21, 2); + let floating_sum: f32 = floating_sum(3.6, 8.8); + let floating_substrt: f32 = floating_substrt(24.6, 8.8); + let make_string: String = make_string("martin", "19"); + + println!("The sum result is {}", result_sum); + println!("The result is {}", result_substrt); + println!("The result is {}", result_division); + println!("The result is {}", result_multiply); + println!("is_even result is {}", is_even); + println!("is_even result is {}", is_sum_even); + println!("floating_sum result is {}", floating_sum); + println!("floating_sum result is {}", floating_substrt); + println!("my name is {}", make_string); + + intro_to_ownable_string(); +} + +pub fn sum(x: u8, y: u8) -> u8 { + x + y + // return x + y; // explicit return +} + +pub fn substraction(x: u16, y: u16) -> u16 { + x - y +} + +pub fn division(x: u16, y: u16) -> u16 { + x / y +} + +pub fn multiply(x: u16, y: u16) -> u16 { + x * y +} + +pub fn is_even(x: u16, y: u16) -> bool { + if x % y == 0 { + return true; + } else { + return false; + } +} + +pub fn is_sum_even(x: u32, y: u32) -> bool { + let is_sum_evens: u32 = x + y; + if is_sum_evens % 2 == 0 { + true + } else { + false + } +} + +pub fn floating_sum(x: f32, y: f32) -> f32 { + x + y +} + +pub fn floating_substrt(x: f32, y: f32) -> f32 { + x - y +} + +pub fn make_string(x: &str, y: &str) -> String { + format!("{x} and i scored {y}") +} + +pub fn intro_to_ownable_string() { + let mut fname: String = String::from("martin"); + println!("first name: {} {}", fname, fname.len()); + + fname.push_str("vibes"); + println!("first name_______: {} {}", fname, fname.len()); +} \ No newline at end of file From 18759fb320a0e04817d5a4bf09de2e976e53cd8d Mon Sep 17 00:00:00 2001 From: martin machiebe Date: Tue, 28 Jan 2025 14:50:11 +0100 Subject: [PATCH 2/2] assignment 3 modularize code --- fp.md | 59 +++++++++++++++++++++++++++ pointer.md | 0 src/charac.rs | 16 ++++++++ src/floating_points.rs | 15 +++++++ src/main.rs | 32 +++++---------- src/primitive_data_types.rs | 79 ------------------------------------- src/signed_integer.rs | 28 +++++++++++++ src/unsigned_integer.rs | 28 +++++++++++++ 8 files changed, 155 insertions(+), 102 deletions(-) create mode 100644 fp.md create mode 100644 pointer.md create mode 100644 src/charac.rs create mode 100644 src/floating_points.rs delete mode 100644 src/primitive_data_types.rs create mode 100644 src/signed_integer.rs create mode 100644 src/unsigned_integer.rs diff --git a/fp.md b/fp.md new file mode 100644 index 0000000..a9fd4fa --- /dev/null +++ b/fp.md @@ -0,0 +1,59 @@ +# Understanding f32 and f64 in Rust + +## Overview + +Rust provides two floating-point types: `f32` (32-bit) and `f64` (64-bit). Both follow the IEEE 754 standard for floating-point arithmetic. + +## Key Differences + +### Precision + +- `f32`: Single precision, ~6-7 decimal digits of precision +- `f64`: Double precision, ~15-17 decimal digits of precision + +### Memory Usage + +- `f32`: 4 bytes +- `f64`: 8 bytes + +### Range + +- `f32`: ±3.4E+38 to ±3.4E-38 +- `f64`: ±1.8E+308 to ±2.2E-308 + +### Use Cases + +#### f32 is preferred when: + +- Working with graphics and game development +- Memory is constrained +- Performance is critical +- Full double precision isn't necessary +- Working with hardware that natively uses 32-bit floats + +#### f64 is preferred when: + +- Higher precision is required +- Working with scientific calculations +- Dealing with large numerical ranges + +### Performance Considerations + +- `f32` operations are generally faster +- Modern CPUs might handle `f64` just as efficiently +- `f32` arrays use less cache space +- SIMD operations can process more `f32` values simultaneously + +### Common Pitfalls + +1. Precision loss in `f32` during complex calculations +2. Comparison issues due to floating-point rounding +3. Accumulated errors in long calculation chains +4. Platform-dependent behavior + +## Best Practices + +1. Use `f64` by default unless you have specific reasons not to +2. Avoid direct equality comparisons +3. Consider using decimal types for financial calculations +4. Document precision requirements in your code diff --git a/pointer.md b/pointer.md new file mode 100644 index 0000000..e69de29 diff --git a/src/charac.rs b/src/charac.rs new file mode 100644 index 0000000..a9477af --- /dev/null +++ b/src/charac.rs @@ -0,0 +1,16 @@ +pub fn char_string() { + let make_string: String = make_string("martin", "19"); + println!("my name is {}", make_string); +} + +pub fn make_string(x: &str, y: &str) -> String { + format!("{x} and i scored {y}") +} + +pub fn intro_to_ownable_string() { + let mut fname: String = String::from("martin"); + println!("first name: {} {}", fname, fname.len()); + + fname.push_str("vibes"); + println!("first name_______: {} {}", fname, fname.len()); +} \ No newline at end of file diff --git a/src/floating_points.rs b/src/floating_points.rs new file mode 100644 index 0000000..ef27edc --- /dev/null +++ b/src/floating_points.rs @@ -0,0 +1,15 @@ +pub fn floating_num() { + let floating_sum: f32 = floating_sum(3.6, 8.8); + let floating_substrt: f32 = floating_substrt(24.6, 8.8); + + println!("floating_sum result is {}", floating_sum); + println!("floating_sum result is {}", floating_substrt); +} + +pub fn floating_sum(x: f32, y: f32) -> f32 { + x + y +} + +pub fn floating_substrt(x: f32, y: f32) -> f32 { + x - y +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 3feb341..42273df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,27 +1,13 @@ -mod primitive_data_types; +mod floating_points; +mod charac; +mod signed_integer; +mod unsigned_integer; fn main() { - intro_to_u(); - primitive_data_types::intro_to_us(); -} - - -// function to encapsulate all integers -fn intro_to_u() { - let sum_result: u8 = sum(5, 10); - println!("the sum result is: {}", sum_result); - - -} + floating_points::floating_num(); + charac::intro_to_ownable_string(); + charac::char_string(); + signed_integer::signed_int(); + unsigned_integer::unsigned_int(); -fn sum(x: u8, y: u8) -> u8 { - x + y // implicit return -// return x + y; // explicit return } - - -// subtract -// multiplication -// division - - diff --git a/src/primitive_data_types.rs b/src/primitive_data_types.rs deleted file mode 100644 index 03a4b9c..0000000 --- a/src/primitive_data_types.rs +++ /dev/null @@ -1,79 +0,0 @@ -use core::convert::Into; - -pub fn intro_to_us() { - let result_sum: u16 = sum(10, 7).into(); - let result_substrt: u16 = substraction(23, 17); - let result_division: u16 = division(25, 5); - let result_multiply: u16 = multiply(23, 17); - let is_even: bool = is_even(22, 2); - let is_sum_even: bool = is_sum_even(21, 2); - let floating_sum: f32 = floating_sum(3.6, 8.8); - let floating_substrt: f32 = floating_substrt(24.6, 8.8); - let make_string: String = make_string("martin", "19"); - - println!("The sum result is {}", result_sum); - println!("The result is {}", result_substrt); - println!("The result is {}", result_division); - println!("The result is {}", result_multiply); - println!("is_even result is {}", is_even); - println!("is_even result is {}", is_sum_even); - println!("floating_sum result is {}", floating_sum); - println!("floating_sum result is {}", floating_substrt); - println!("my name is {}", make_string); - - intro_to_ownable_string(); -} - -pub fn sum(x: u8, y: u8) -> u8 { - x + y - // return x + y; // explicit return -} - -pub fn substraction(x: u16, y: u16) -> u16 { - x - y -} - -pub fn division(x: u16, y: u16) -> u16 { - x / y -} - -pub fn multiply(x: u16, y: u16) -> u16 { - x * y -} - -pub fn is_even(x: u16, y: u16) -> bool { - if x % y == 0 { - return true; - } else { - return false; - } -} - -pub fn is_sum_even(x: u32, y: u32) -> bool { - let is_sum_evens: u32 = x + y; - if is_sum_evens % 2 == 0 { - true - } else { - false - } -} - -pub fn floating_sum(x: f32, y: f32) -> f32 { - x + y -} - -pub fn floating_substrt(x: f32, y: f32) -> f32 { - x - y -} - -pub fn make_string(x: &str, y: &str) -> String { - format!("{x} and i scored {y}") -} - -pub fn intro_to_ownable_string() { - let mut fname: String = String::from("martin"); - println!("first name: {} {}", fname, fname.len()); - - fname.push_str("vibes"); - println!("first name_______: {} {}", fname, fname.len()); -} \ No newline at end of file diff --git a/src/signed_integer.rs b/src/signed_integer.rs new file mode 100644 index 0000000..0f34323 --- /dev/null +++ b/src/signed_integer.rs @@ -0,0 +1,28 @@ +pub fn signed_int() { + let result_sum: i32 = sum(10, 7).into(); + let result_substrt: i32 = substraction(23, 17); + let result_division: i32 = division(25, 5); + let result_multiply: i32 = multiply(23, 17); + + println!("The sum result is {}", result_sum); + println!("The result is {}", result_substrt); + println!("The result is {}", result_division); + println!("The result is {}", result_multiply); +} + +pub fn sum(x: i32, y: i32) -> i32 { + x + y + // return x + y; // explicit return +} + +pub fn substraction(x: i32, y: i32) -> i32 { + x - y +} + +pub fn division(x: i32, y: i32) -> i32 { + x / y +} + +pub fn multiply(x: i32, y: i32) -> i32 { + x * y +} \ No newline at end of file diff --git a/src/unsigned_integer.rs b/src/unsigned_integer.rs new file mode 100644 index 0000000..8756800 --- /dev/null +++ b/src/unsigned_integer.rs @@ -0,0 +1,28 @@ +pub fn unsigned_int() { + let result_sum: u32 = sum(10, 7).into(); + let result_substrt: u32 = substraction(23, 17); + let result_division: u32 = division(25, 5); + let result_multiply: u32 = multiply(23, 17); + + println!("The sum result is {}", result_sum); + println!("The result is {}", result_substrt); + println!("The result is {}", result_division); + println!("The result is {}", result_multiply); +} + +pub fn sum(x: u32, y: u32) -> u32 { + x + y + // return x + y; // explicit return +} + +pub fn substraction(x: u32, y: u32) -> u32 { + x - y +} + +pub fn division(x: u32, y: u32) -> u32 { + x / y +} + +pub fn multiply(x: u32, y: u32) -> u32 { + x * y +} \ No newline at end of file