Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a float can be converted to integer without loss

Tags:

idioms

rust

I wanted to check whether an integer was a power of 2. My standard approach would have been to see if log₂(x) was an integer value, however I found no elegant way to do this. My approaches were the following:

let x = 65;

let y = (x as f64).log(2.0);

// Compute the difference between y and the result of
// of truncating y by casting to int and back
let difference = y - (y as i64 as f64);

// This looks nice but matches on float values will be phased out
match difference {
    0.0 => println!("{} is a power of 2", x),
    _ => println!("{} is NO power of 2", x),
}

// This seems kind of clunky
if difference == 0.0 {
    println!("{} is a power of 2", x);
} else {
    println!("{} is NO power of 2", x);
}

Is there a builtin option in Rust to check if a float can be converted to an integer without truncation?

Something that behaves like:

42.0f64.is_int() // True/ Ok()
42.23f64.is_int() // False/ Err()

In other words, a method/ macro/ etc. that allows me to check if I will lose information (decimals) by casting to int.


I already found that checking whether an integer is a power of 2 can be done efficiently with x.count_ones() == 1.

like image 816
m00am Avatar asked Jan 29 '18 11:01

m00am


People also ask

Can float be converted to integer?

A float value can be converted to an int value no larger than the input by using the math. floor() function, whereas it can also be converted to an int value which is the smallest integer greater than the input using math. ceil() function.

How do you check if a float is an integer?

Check if float is integer: is_integer() float has is_integer() method that returns True if the value is an integer, and False otherwise. For example, a function that returns True for an integer number ( int or integer float ) can be defined as follows. This function returns False for str .

Can we convert float to int in Python?

Python also has a built-in function to convert floats to integers: int() . In this case, 390.8 will be converted to 390 . When converting floats to integers with the int() function, Python cuts off the decimal and remaining numbers of a float to create an integer.

What will happen if you cast a float to an integer?

Convert a float to an int always results in a data loss. The trunc() function returns the integer part of a number. The floor() function returns the largest integer less than or equal to a number. The ceil() function returns the smallest integer greater than or equal to a number.

How to convert a float value to an int value?

A float value can be converted to an int value no larger than the input by using the math.floor() function, whereas it can also be converted to an int value which is the smallest integer greater than the input using math.ceil() function. The math module is to be imported in order to use these methods. Syntax: math.floor(x) Parameter:

How to check if string is float number?

This works in 2 steps, first the point value is erased and the string is joined to form a digit and then is checked. The drawback is that this doesn’t check for potential exponent values that can also form a float number. The original string : 45.657 Is string a possible float number ?

Is it safe to convert int to floating point?

However, it is to be noted that such type of conversion may tend to be a lossy one (loss of data). Converting an int value like 2 to floating-point will result in 2.0, such types of conversion are safe as there would be no loss of data, but converting 3.4 to an int value will result in 3 leading to a lossy conversion.

How to convert a variable to an integer in Python?

The int () function converts the given value to an integer value. If no arguments are passed in the function, it returns zero. The default base is set as 10 if no base value is given. An integer object can be constructed using a number or a string. Let us see a simple example to print the integer value of a given variable.


1 Answers

You can use fract to check if there is a non-zero fractional part:

42.0f64.fract() == 0.0;
42.23f64.fract() != 0.0;

Note that this only works if you already know that the number is in range. If you need an extra check to test that the floating-point number is between 0 and u32::MAX (or between i32::MIN and i32::MAX), then you might as well do the conversion and check that it didn't lose precision:

x == (x as u32) as f64
like image 81
Jmb Avatar answered Oct 14 '22 06:10

Jmb