cancel
Showing results for 
Search instead for 
Did you mean: 

How to tell the compiler to use float instead of double for constants.

Julien Bouille
Associate III
Posted on December 22, 2017 at 20:24

Hi,

i noticed then when i declare a constant like this

#define PI 3.141592653

compiler use double instead of float wich is slowing down code execution. Is there a way to tell compiler to use float by default. instread of casting everywere in my code ?

I use keil µVision and C51

Tanks

4 REPLIES 4
Posted on December 22, 2017 at 20:39

float pi = 3.14159f;

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Mark Peter Vargha
Associate III
Posted on December 23, 2017 at 05:55

This also works with define.

#define PI   3.14159f

S.Ma
Principal
Posted on December 23, 2017 at 08:01

If you really want to optimize, you'll use something like:

#define PI_x10k = 314159

In practice, most android low level drivers use a format called Q31: 32 bit, 16 bit integers, 16 bit fractional.

1 = 0x0001 0000

0.25 = 0x0000 0100

4 = 0x0004 0000

Andrew Neil
Chief II
Posted on December 24, 2017 at 02:58

I use keil µVision and C51

You can't use C51 with STM32!!

:(