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 3cc3d3e..42273df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,24 +1,13 @@ -fn main() { - intro_to_u(); -} - +mod floating_points; +mod charac; +mod signed_integer; +mod unsigned_integer; -// function to encapsulate all integers -fn intro_to_u() { - let sum_result: u8 = sum(5, 10); - println!("the sum result is: {}", sum_result); - - -} +fn main() { + 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/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