Re: How to use single precision floating point?
On Aug 7, 3:14 am, Immortal Nephi <Immortal_Ne...@hotmail.com> wrote:
I want to use single precision floating point. I don't need
to use double precision floating point because single
precision floating point is faster.
Really? On what platform. Historically, the reason C++
defaults to double is that C did, and the reason C defaults to
double is that double was significantly faster on the most
important platforms of the time. I would be very surprised if
there were an important modern platform where it made a
difference, and if it did, I suspect that it is double that
would be faster.
How do I tell C++ Compiler to treat all floating point numbers
with float data type instead of double data type?
"-Ddouble=float". Doing so will, of course, mean that you can't
use any existing library (including the system libraries or the
standard C++ libraries).
I don't need to use casting operator.
For example:
float x = 1.0 / 3.5;
1.0 / 3.5 always use double data type.
With most compilers, the arithmetic will be done at compile
time, not run-time. And the results rounded to float.
I'm not sure what you're getting at here---if you want floating
point constants to be float, and not double. If you want a
literal to have type float, just suffix it with and 'F', e.g.
3.5F. But I can't imagine why one would want to do such a
thing. (And many compilers will promote float to double in all
arithmetic expressions. On an Intel architecture, in fact,
arithmetic operations will always be done in a long double
format, 10 bytes. You can configure the processor to use less,
but it runs slower if you do.)
Doing casting operator is annoying.
float x = float( 1.0 / 3.5 ); // Too annoying
That conversion is implicit anyway, so there's no point to do
it.
--
James Kanze