A couple of questions re variables on the Teensy3

Status
Not open for further replies.

Constantin

Well-known member
I read over at Adafruit about the space requirements for various variables, including booleans. It appears that on the AVR platform, they always consume a byte of memory. (And here I thought that booleans take up less space and hence used them as much as I could)

Are booleans treated similarly re SRAM needs on the Teensy 3? Does the answer change if you were to use an array of them?

Also, since the Teensy 3 lacks a dedicated FPU, I have tried to use integer math as much as possible. But floating points do creep in here and there... So I also want to confirm that the Teensy 3 double is in fact differentiated from a float, unlike on the Arduino platform?
 
C/C++ does not have the concept for arrays of bits, so a bool type will take at least one byte for each element.

On the Teensy, each int takes 4 bytes, while on the AVR, each int takes 2 bytes.

As El_supremo says, each double takes 8 bytes. Note, due to the nature of C being designed on PDP-7/11's many years ago, a floating point constant is always considered to be a double, unless you use a suffix ('f' or 'F' for 32-bit float, 'l' or 'L' for long double). This means if you have code of the form:
Code:
float f, g;
// ...
f = g + 2.0;

The compiler will likely convert g to double, do the arithmetic in 64-bit double, and convert the result to be float So, if you use float, you want to decorate each floating point constant used with a f suffix. I.e.
Code:
float f, g;
// ...
f = g + 2.0f;

Similarly, if you use math functions like sinh, sqrt, etc. You want to use the version that is targeted towards floats, i.e. sinhf, sqrtf.

A future Teensy 3.x will have single precision floating hardware support, so if you change to use float, you program should speed up.
 
nit:
I read that this yields a double floating point sized constant:

1234.56L
Technically it yields a long double, which the ISO standards require to be at least as big as a double. I believe the 32-bit arm target defines long double to be the same size as double. The PowerPC, x86, arm 64-bit, PA, Spark, MIPS, and Alpha ports define long double to be a bigger type.

PowerPC currently defines long double to be a type with a pair of doubles, which gives more precision, but not extended exponent range. I have been working off and on to optionally make it IEEE 754 compliant. X86 by default uses the 80-bit Intel floating point format that the 80387 coprocessor on the 386 originally used.. X86 has an optional type __float128 that provides IEEE 754 support, using the suffix Q or q, and my work adds similar support to the PowerPC.
 
Status
Not open for further replies.
Back
Top