Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rust 'for loop' (converting from c++)

Tags:

rust

Trying to convert this for loop from c++ to rust and i'm having a hard time figuring it out as I'm very new to Rust syntax.

double sinError = 0;
for (float x = -10 * M_PI; x < 10 * M_PI; x += M_PI / 300) {
    double approxResult = sin_approx(x);
    double libmResult = sinf(x);
    sinError = MAX(sinError, fabs(approxResult - libmResult));
}
like image 373
blindside044 Avatar asked Oct 23 '25 14:10

blindside044


1 Answers

Iterate over integers

As @trentcl already pointed out, it's usually better to iterate over integers instead of floats, to prevent numerical errors from adding up:

use std::f32::consts::PI;

let mut sin_error = 0.0;

for x in (-3000..3000).map(|i| (i as f32) * PI / 300.0) {
    sin_error = todo!();
}

Just replace todo!() with the code that computes the next sin_error.

A more functional way

use std::f32::consts::PI;

let sin_error = (-3000..3000)
    .map(|i| (i as f32) * PI / 300.0)
    .fold(0.0, |sin_error, x| todo!());

In case you don't care about numerical errors, or want to iterate over something else, here are some other options:

Use a while loop

It's not as nice, but does the job!

use std::f32::consts::PI;

let mut sin_error = 0.0;
let mut x = -10.0 * PI;

while (x < 10.0 * PI) {
    sin_error = todo!();
    x += PI / 300.0;
}

Create your iterator with successors()

The successors() function creates a new iterator where each successive item is computed based on the preceding one:

use std::f32::consts::PI;
use std::iter::successors;

let mut sin_error = 0.0;

let iter = successors(Some(-10.0 * PI), |x| Some(x + PI / 300.0));

for x in iter.take_while(|&x| x < 10.0 * PI) {
    sin_error = todo!();
}

A more functional way

use std::f32::consts::PI;
use std::iter::successors;

let sin_error = successors(Some(-10.0 * PI), |x| Some(x + PI / 300.0))
   .take_while(|&x| x < 10.0 * PI)
   .fold(0.0, |sin_error, x| todo!());
like image 185
Aloso Avatar answered Oct 26 '25 07:10

Aloso



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!