Use of int32_t

My IDE is 1.6.4. The code lines impacted are:
void sendFrequency(double frequency) {
int32_t freq = frequency * 4294967296 / 180.0e6;
for (int b=0; b<4; b++, freq>>=8) {
tfr_byte(freq & 0xFF);

The compiler does not recognize int32_t. According to the C++ site, this is a questionable instruction to use. Is there another way to implement this calculation which is compatible with this IDE?

unsigned long
use in place of "double" and "int32"

You need to include

#include <inttypes.h>

fdwcon:
The compiler does not recognize int32_t. According to the C++ site, this is a questionable instruction to use. Is there another way to implement this calculation which is compatible with this IDE?

You need to include

#include <inttypes.h>

Which C++ site because the use of int types is designed to make C programs portable.

Is this ok?
180.0e6
maybe use 180000000UL instead

or
float freq = frequency * 4294967296 / 180000000.0;
if need decimal places?

CrossRoads:
Is this ok?
180.0e6
maybe use 180000000UL instead

or
float freq = frequency * 4294967296 / 180000000.0;
if need decimal places?

the float will give a more accurate division,
the unsigned long will truncate (round down)

Shouldn't it be 4294967296.0? Or 4294967296LL? Maximum value in a 32-bit int is 4294967295 (there are 4294967296 numbers possible, but 0 is one of them)...

// frequency is a double

int32_t freq = frequency * 4294967296 / 180.0e6;

That calculation is going to be done using floating point arithmetic. Which means that your fancy magic number on the right hand side is going to be truncated to about 6 decimal digits of precision anyway.

You are probably better off calculating the result of the division of the second and third terms in that expression yourself, where you can use any other computer which has real doubles and you know exactly what you will get.

int32_t freq = frequency * 4294967296 / 180.0e6;

Better would be:

int32_t freq = frequency * (4294967296.0 / 180.0e6);

michinyon:

// frequency is a double

int32_t freq = frequency * 4294967296 / 180.0e6;




That calculation is going to be done using floating point arithmetic. Which means that your fancy magic number on the right hand side is going to be truncated to about 6 decimal digits of precision anyway.

You are probably better off calculating the result of the division of the second and third terms in that expression yourself, where you can use any other computer which has real doubles and you know exactly what you will get.

Or you can use parentheses and have the compiler do it. But I'm not sure it will accept 4294967296 as such, or if it will want a decimal point, like this: 4294967296.0

The problem with "letting the compiler do it", is that it is not easy to figure out what the compiler will do, unless you are a super-guru on that compiler.

As someone pointed out, that magic number 4294967296 is exactly one more than the biggest number you can fit in an unsigned long. It is 0x100000000 . Now, there might be some reason for that, which is not apparent, without knowing the context of this code more, and the machine for which it was originally written.

If this was my problem, I'd be trying to figure out what that calculation is actually trying to achieve, and solve it another way, than spends days rooting around in the turgid compiler manual trying to figure out exactly how it will precalculate that formula.

Well, it could be

int32_t freq = frequency * 23.86092942222222;

Good point.

In fact, the smart way to evaluate x 4294967296 would be to add 32 to the binary value of the exponent component of the double.

Or some kind of left shift.