Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the function to get the quotient and remainder (DIVMOD) for rust?

Tags:

division

rust

mod

x86 and likely other architectures provide a method to get the quotient and remainder in a single operation (DIV). Because of this many languages have a DIVMOD combined operation, (like DIVREM in C#, DIVMOD in Python, or with div and div_t in C. How can I do this in Rust?

Is there an optimized method to perform both

let res = (a / b, a % b);
like image 719
NO WAR WITH RUSSIA Avatar asked Oct 27 '22 11:10

NO WAR WITH RUSSIA


1 Answers

As rodrigo commented already, the compiler is able to optimize this away. For the sake of completeness, there is a num_integer::div_rem method if you need it for generic integer types, but I'd vote against using this library if you don't need to be generic

like image 104
mineichen Avatar answered Dec 15 '22 04:12

mineichen