Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions fp.md
Original file line number Diff line number Diff line change
@@ -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
Empty file added pointer.md
Empty file.
16 changes: 16 additions & 0 deletions src/charac.rs
Original file line number Diff line number Diff line change
@@ -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());
}
15 changes: 15 additions & 0 deletions src/floating_points.rs
Original file line number Diff line number Diff line change
@@ -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
}
31 changes: 10 additions & 21 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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


28 changes: 28 additions & 0 deletions src/signed_integer.rs
Original file line number Diff line number Diff line change
@@ -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
}
28 changes: 28 additions & 0 deletions src/unsigned_integer.rs
Original file line number Diff line number Diff line change
@@ -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
}