C: converting Farenheit to Celsius

The scanf call uses the wrong format string. You are reading an int so you need it to be: scanf(“%d”, &fahrenheit); The expression 5/9 is evaluated using integer division. In fact the compiler can work it out at compile time. That expression evaluates to 0. You need to perform floating point division. For instance: 5.0/9 … Read more

How to compute 2⁶⁴/n in C?

I’ll use uint64_t here (which needs the <stdint.h> include) so as not to require your assumption about the size of unsigned long. phuclv’s idea of using -n is clever, but can be made much simpler. As unsigned 64-bit integers, we have -n = 264-n, then (-n)/n = 264/n – 1, and we can simply add … Read more

Fast Division on GCC/ARM

After that, however, it subtracted (dividend/2^31) from the result. Actually, it subtracts dividend >> 31, which is -1 for negative dividend, and 0 for non-negative dividend, when right-shifting negative integers is arithmetic right-shift (and int is 32 bits wide). 0x6666667 = (2^34 + 6)/10 So for x < 0, we have, writing x = 10*k … Read more

How to perform division in Go

The operands of the binary operation 3 / 10 are untyped constants. The specification says this about binary operations with untyped constants if the operands of a binary operation are different kinds of untyped constants, the operation and, for non-boolean operations, the result use the kind that appears later in this list: integer, rune, floating-point, … Read more