Dynamic typedef primitive in C ++

I am creating a toy physics engine that works with floating point numbers, which I call realities.

I am currently using typedef;

typedef float real;

That way, I can change the precision of the floating point values ​​to double or double, but obviously I have to recompile. I would like to be able to clearly determine the type of real at runtime, so that I can specify the accuracy through the command line or interface interface interface.

I know that typedef is determined at compile time, so I wonder if anyone has any neat ideas.

+3
source share
2 answers

A float uses less memory than double, and is less accurate.

, , . double , float.

.

+2

@Kerrek SB, , float double s.

, , , , .

template <typename T>
T Crunch1(T rhs)
{
    // do something with 'rhs' and return the result
}
template <typename T>
T Crunch2(T lhs, T rhs)
{
    // do something with 'lhs' & 'rhs' and return the result
}

- , , , .

( ) , , . . , , , , , .

+2

All Articles