Why do I get an error when adding an integer to a floating point?

The technically correct answer is: because no one has written impl Add<f64> for i32 {}. The cheeky answer is: because Rust doesn’t want you to shoot yourself in the foot. The longer, potentially more useful answer is… In computers, integers and floating point numbers both have a limited range, ultimately driven by the number of … Read more

Is integer division always equal to the floor of regular division?

The reason the quotients in your test case are not equal is that in the math.floor(a/b) case, the result is calculated with floating point arithmetic (IEEE-754 64-bit), which means there is a maximum precision. The quotient you have there is larger than the 253 limit above which floating point is no longer accurate up to … Read more

How to read an integer input from the user in Rust 1.0?

Here is a version with all optional type annotations and error handling which may be useful for beginners like me: use std::io; fn main() { let mut input_text = String::new(); io::stdin() .read_line(&mut input_text) .expect(“failed to read from stdin”); let trimmed = input_text.trim(); match trimmed.parse::<u32>() { Ok(i) => println!(“your integer input: {}”, i), Err(..) => println!(“this … Read more

Differences between new Integer(123), Integer.valueOf(123) and just 123

new Integer(123) will create a new Object instance for each call. According to the javadoc, Integer.valueOf(123) has the difference it caches Objects… so you may (or may not) end up with the same Object if you call it more than once. For instance, the following code: public static void main(String[] args) { Integer a = … Read more

How to convert a string of space- and comma-separated numbers into a list of int

Split on commas, then map to integers: map(int, example_string.split(‘,’)) Or use a list comprehension: [int(s) for s in example_string.split(‘,’)] The latter works better if you want a list result, or you can wrap the map() call in list(). This works because int() tolerates whitespace: >>> example_string = ‘0, 0, 0, 11, 0, 0, 0, 0, … Read more

Should I use %i or %d to print an integer in C using printf()?

They are completely equivalent when used with printf(). Personally, I prefer %d. It’s used more often (should I say “it’s the idiomatic conversion specifier for int“?). (One difference between %i and %d is that when used with scanf(), then %d always expects a decimal integer, whereas %i recognizes the 0 and 0x prefixes as octal … Read more

Difference between parseInt() and valueOf() in Java?

Well, the API for Integer.valueOf(String) does indeed say that the String is interpreted exactly as if it were given to Integer.parseInt(String). However, valueOf(String) returns a new Integer() object whereas parseInt(String) returns a primitive int. If you want to enjoy the potential caching benefits of Integer.valueOf(int), you could also use this eyesore: Integer k = Integer.valueOf(Integer.parseInt(“123”)) … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)