How can I convert a f64 into the closest f32?
There is neither a From nor a TryFrom implementation, presumably because such implementations are only provided for lossless conversions. I also searched for references to f32 in the f64 docs and vice versa, and found nothing.
let double: f64 = 0.;
// doesn't work
// let single: f32 = double.into();
// let single: f32 = double.try_into().unwrap();
In this case, we can use the as keyword for casting. It is usually not desired for integer conversions because "casting from a larger integer to a smaller integer (e.g. u32 -> u8) will truncate", and this is usually not wanted. For f64 to f32, however, it's very sensible:
Casting from an f64 to an f32 will produce the closest possible f32
if necessary, rounding is according to roundTiesToEven mode
on overflow, infinity (of the same sign as the input) is produced
Rust Reference
let double: f64 = 42.;
dbg!(double as f32);
[src/main.rs:13] double as f32 = 42.0
If an overflow to infinity is not desired and you want to panic instead, the simplest solution might be to check for is_finite:
fn f64_to_f32(x: f64) -> f32 {
let y = x as f32;
assert_eq!(
x.is_finite(),
y.is_finite(),
"f32 overflow during conversion"
);
y
}
fn main() {
dbg!(f64_to_f32(42_f64));
// dbg!(f64_to_f32(f64::MAX / 10.)); // panics
dbg!(f64_to_f32(f64::NAN));
dbg!(f64_to_f32(f64::INFINITY));
dbg!(f64_to_f32(f64::NEG_INFINITY));
}
[src/main.rs:2] f64_to_f32(42_f64) = 42.0
[src/main.rs:4] f64_to_f32(f64::NAN) = NaN
[src/main.rs:5] f64_to_f32(f64::INFINITY) = inf
[src/main.rs:6] f64_to_f32(f64::NEG_INFINITY) = -inf
Related
This question already has answers here:
From and Into traits and conversion of usize to f64
(2 answers)
Closed 2 years ago.
I have implemented the following function that calculates the mean of a sequence:
fn mean<T, R>(seq: &[T]) -> R
where R: Div<R, Output=R> + From<T> + From<usize> + Sum<R>
{
let total: R = seq.iter().map(|&x| R::from(x)).sum()
let size = R::from(seq.len())
total / size
}
However, I'm having trouble when converting the usize that is returned from seq.len()
let numbers = vec![10, 20, 30, 40, 50, 60];
let result: f32 = mean(&numbers);
println!("{:?}", result);
5 | fn mean<T, R>(seq: &[T]) -> R
| ----
6 | where R: Div<R, Output=R> + From<T> + From<usize> + Sum<R>
| ----------- required by this bound in `mean`
...
15 | let result: f32 = mean(&numbers);
| ^^^^ the trait `std::convert::From<usize>` is not implemented for `f32`
I'm quite blocked by this since I'm still a beginner with traits. How can I solve this specific problem? (if it can be done)
It is not possible to represent every possible i32 value in f32 format.
That's the reason why From trait implementation provided for f32 supports only i16/u16/i8/u8.
i32 and f32 have the same amount of bytes but usually f32 spent few bytes for exponent so it can't represent all numbers from i32. Single-precision floating-point format
The problem is that f32 does not implement From<usize>. It does implement From<i16>, From<i8>, From<u16> and From<u8>. This is becausef32` cannot represent all the values of a bigger integer exactly.
What you probably want is to use the as conversion, that allows for some precision loss. Unfortunately you cannot use as on generic types, only on primitive types.
You could write a trait to do all the necessary as conversions manually... But of course there is a crate for that! With num_traits::cast::AsPrimititve your code becomes:
fn mean<T, R>(seq: &[T]) -> R
where R: Div<R, Output=R> + Sum<R> + Copy + 'static,
T: AsPrimitive<R>,
usize: AsPrimitive<R>
{
let total: R = seq.iter().map(|&x| x.as_()).sum();
let size = seq.len().as_();
total / size
}
I have added the Copy constraints, that I think you missed in your code, and also the 'static for R that is required for AsPrimitive.
I have an ASCII string slice and I need to compute the sum of all characters when seen as bytes.
let word = "Hello, World";
let sum = word.as_bytes().iter().sum::<u8>();
I need to specify the type for sum, otherwise Rust will not compile. The problem is that u8 is a too small type, and if the sum overflows the program will panic.
I'd like to avoid that, but I cannot find a way to specify a bigger type such as u16 or u32 for example, when using sum().
I may try to use fold(), but I was wondering if there is a way to use sum() by specifying another type.
let sum = word.as_bytes().iter().fold(0u32, |acc, x| acc + *x as u32);
You can use map to cast each byte to a bigger type:
let sum: u32 = word.as_bytes().iter().map(|&b| b as u32).sum();
or
let sum: u32 = word.as_bytes().iter().cloned().map(u32::from).sum();
The reason why you can't sum to u32 using your original attempt is that the Sum trait which provides it has the following definition:
pub trait Sum<A = Self> {
fn sum<I>(iter: I) -> Self
where
I: Iterator<Item = A>;
}
Which means that its method sum returns by default the same type as the items of the iterator it is built from. You can see it's the case with u8 by looking at its implementation of Sum:
fn sum<I>(iter: I) -> u8
where
I: Iterator<Item = u8>,
I tried to implement a small module where I calculate the mean of a vector:
pub mod vector_calculations {
pub fn mean(vec: &Vec<i32>) -> f32 {
let mut sum: f32 = 0.0;
for el in vec.iter() {
sum = el + sum;
}
sum / vec.len()
}
}
As far as I can tell from the compiler error, there are two problems with my code:
error[E0277]: the trait bound `&i32: std::ops::Add<f32>` is not satisfied
--> src/main.rs:6:22
|
6 | sum = el + sum;
| ^ no implementation for `&i32 + f32`
|
= help: the trait `std::ops::Add<f32>` is not implemented for `&i32`
error[E0277]: the trait bound `f32: std::ops::Div<usize>` is not satisfied
--> src/main.rs:9:13
|
9 | sum / vec.len()
| ^ no implementation for `f32 / usize`
|
= help: the trait `std::ops::Div<usize>` is not implemented for `f32`
I'm trying to add a &i32 with a f32 and I'm trying to divide a f32 with an usize.
I could solve the second error by changing the last line to:
sum / (vec.len() as f32)
Is this is actually how a Rust programmer would do this?
Furthermore, I don't really know how to solve the first error. What has to be done and why?
Yes, dereferencing values and converting numeric types is normal in Rust. These conversions help the programmer recognize that edge cases are possible. As loganfsmyth points out:
An i32 can hold values greater than f32 can represent accurately
Unfortunately, the compiler can't tell what's "correct" for your case, so you still have to be on guard.
For what it's worth, I'd write your current implementation using Iterator::sum:
fn mean(items: &[i32]) -> f64 {
let sum: f64 = items.iter().map(|&v| v as f64).sum();
sum / (items.len() as f64)
}
You should also probably handle the case where the input is empty to avoid dividing by zero:
fn mean(items: &[i32]) -> Option<f64> {
let len = items.len();
if len == 0 {
None
} else {
let sum: f64 = items.iter().map(|&v| v as f64).sum();
Some(sum / (len as f64))
}
}
Using the method from What is a good solution for calculating an average where the sum of all values exceeds a double's limits?, but made a bit more iterator-heavy:
fn mean2(ary: &[i32]) -> f64 {
ary.iter().enumerate().fold(0.0, |avg, (i, &x)| {
avg + ((x as f64 - avg) / (i + 1) as f64)
})
}
See also:
Why is it discouraged to accept a reference to a String (&String) or Vec (&Vec) as a function argument?
.iter() returns an &i32 and Rust does not automatically dereference for type conversions — you are currently trying to change the pointer (&) instead of changing what it's pointing to.
Changing your code to look like this is the simplest way to make it work:
pub mod vector_calculations {
pub fn mean(vec: &Vec<i32>) -> f32 {
let mut sum: f32 = 0.0;
for el in vec.iter() {
sum = *el as f32 + sum; // first dereference the pointer, than cast to f32
}
sum / vec.len() as f32 // cast to f32
}
}
But there are some ways to improve this kind of code:
pub mod vector_calculations {
pub fn mean(vec: &[i32]) -> f32 { // accept a slice instead of a vector
// it now allows arrays, slices, and vectors
// but now you can't add or remove items
// during this function call.
let mut sum: i32 = 0; // as the sum is still a whole number, changing the type
// should make it slightly easier to understand.
for el in vec.iter() {
sum = el + sum; // now this works without changing the type of el
// you don't even need to dereference el anymore
// as Rust does it automatically.
}
sum as f32 / vec.len() as f32 // now you need to cast to f32 twice at the end
}
}
I found a function to compute a mean and have been playing with it. The code snippet below runs, but if the data inside the input changes from a float to an int an error occurs. How do I get this to work with floats and integers?
use std::borrow::Borrow;
fn mean(arr: &mut [f64]) -> f64 {
let mut i = 0.0;
let mut mean = 0.0;
for num in arr {
i += 1.0;
mean += (num.borrow() - mean) / i;
}
mean
}
fn main() {
let val = mean(&mut vec![4.0, 5.0, 3.0, 2.0]);
println!("The mean is {}", val);
}
The code in the question doesn't compile because f64 does not have a borrow() method. Also, the slice it accepts doesn't need to be mutable since we are not changing it. Here is a modified version that compiles and works:
fn mean(arr: &[f64]) -> f64 {
let mut i = 0.0;
let mut mean = 0.0;
for &num in arr {
i += 1.0;
mean += (num - mean) / i;
}
mean
}
We specify &num when looping over arr, so that the type of num is f64 rather than a reference to f64. This snippet would work with both, but omitting it would break the generic version.
For the same function to accept floats and integers alike, its parameter needs to be generic. Ideally we'd like it to accept any type that can be converted into f64, including f32 or user-defined types that defin such a conversion. Something like this:
fn mean<T>(arr: &[T]) -> f64 {
let mut i = 0.0;
let mut mean = 0.0;
for &num in arr {
i += 1.0;
mean += (num as f64 - mean) / i;
}
mean
}
This doesn't compile because x as f64 is not defined for x of an arbitry type. Instead, we need a trait bound on T that defines a way to convert T values to f64. This is exactly the purpose of the Into trait; every type T that implements Into<U> defines an into(self) -> U method. Specifying T: Into<f64> as the trait bound gives us the into() method that returns an f64.
We also need to request T to be Copy, to prevent reading the value from the array to "consume" the value, i.e. attempt moving it out of the array. Since primitive numbers such as integers implement Copy, this is ok for us. Working code then looks like this:
fn mean<T: Into<f64> + Copy>(arr: &[T]) -> f64 {
let mut i = 0.0;
let mut mean = 0.0;
for &num in arr {
i += 1.0;
mean += (num.into() - mean) / i;
}
mean
}
fn main() {
let val1 = mean(&vec![4.0, 5.0, 3.0, 2.0]);
let val2 = mean(&vec![4, 5, 3, 2]);
println!("The means are {} and {}", val1, val2);
}
Note that this will only work for types that define lossless conversion to f64. Thus it will work for u32, i32 (as in the above example) and smaller integer types, but it won't accept for example a vector of i64 or u64, which cannot be losslessly converted to f64.
Also note that this problem lends nicely to functional programming idioms such as enumerate() and fold(). Although outside the scope of this already longish answer, writing out such an implementation is an exercise hard to resist.
I am trying to use a bit shift but I need the result as an f64. I can't seem to figure how how to shift and let the result as an f64 without making an ugly tmp varible.
let num_bits: uint = 32; // just for reference
// how can these two lines be 1 line
let muli: int = 1<<(num_bits-2);
let mul: f64 = muli as f64;
How can I write the last two line as one line so I don't need muli?
I have tried made various attempts in the theme of:
let m: f64 = 1<<(num_bits-2) as f64;
which gives playpen
<anon>:8:21: 8:40 error: mismatched types: expected `uint`, found `f64` (expected uint, found f64)
<anon>:8 let m: f64 = 1<<(num_bits-2) as f64;
You can do it by annotating the type of the 1 literal. I'm assuming you want it the shift result to be an int (before converting to f64) since you said multi: int. Otherwise, you want 1u.
let m: f64 = (1i << (num_bits - 2)) as f64;
Check the playpen.
If you look at rust reference you can see that as operator has higher precedence than << so you have to do:
fn main () {
let num_bits: uint = 32; // just for reference
let m: f64 = (1u << num_bits - 2) as f64;
println!("mul {}", mul);
}
You also must specify the bype of 1 as uint (1u) because the compiler cannot deretmine the type for it from the context when written that way.