What's the point of floats in C?

Started by
8 comments, last by frob 10 years ago

I don't understand why there is a type float if all floats are promoted to doubles when they are passed as arguments to the function parameters. Unless one is not using functions at all(which is not the case for professional applications), floats are meaningless.

Intel Core 2 Quad CPU Q6600, 2.4 GHz. 3GB RAM. ATI Radeon HD 3400.
Advertisement

What makes you think that all floats are promoted to doubles?

You must have read something that was about variadoc parameters, e.g. printf. That only applies to functions that can take an arbitrary number of parameters. Normal functions don't do that unless the function specifically says to use doubles. An explanation for why is here. The other thing you might be confused about is how the literal 0.0 is a double. If you want a float literal you have to specifiy 0.0f. I don't know why the language design went with double as default over float as default for literals.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

You must have read something that was about variadoc parameters, e.g. printf. That only applies to functions that can take an arbitrary number of parameters. Normal functions don't do that unless the function specifically says to use doubles. An explanation for why is here. The other thing you might be confused about is how the literal 0.0 is a double. If you want a float literal you have to specifiy 0.0f. I don't know why the language design went with double as default over float as default for literals.

Ups! you are right, I miss read it here.

Intel Core 2 Quad CPU Q6600, 2.4 GHz. 3GB RAM. ATI Radeon HD 3400.

Even though the following code prints "8", it does not mean that every float is promoted to a double when passed as arguments if said argument is declared to be of type float.


std::cout << sizeof(1.0) << std::endl;

C is allowed to do one implicit conversion, which makes this valid:


float x = 10; /* valid */

void foo( float y ) {}
foo( 20 ); /* valid */

The numbers 10 and 20 are integers, but that doesn't mean x and y magically become integers too.


I don't know why the language design went with double as default over float as default for literals.

Probably the same reason as with non floating point datatypes: Largest by default.

I'm not on a 64 bit machine, but I assume this would print "8" as well:


std::cout << sizeof(1) << std::endl;
"I would try to find halo source code by bungie best fps engine ever created, u see why call of duty loses speed due to its detail." -- GettingNifty

Actually most 64-bit compilers will print 4. 1 will have type int, which is four bytes on most 64 bit compilers.

Ah yes, quite correct.

http://en.cppreference.com/w/cpp/language/types

"I would try to find halo source code by bungie best fps engine ever created, u see why call of duty loses speed due to its detail." -- GettingNifty

Even though the following code prints "8", it does not mean that every float is promoted to a double when passed as arguments if said argument is declared to be of type float.

That is a skewed test case for proving your point. It prints 8 because 1.0 is a double. If you had used sizeof( 1.0f ) it would print 4.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

in x64 the first 4 floats are passed through the XMM0 - XMM3 registers which which are actually 128bits wide and are SSE registers. The rest are passed through the stack.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

in x64 the first 4 floats are passed through the XMM0 - XMM3 registers which which are actually 128bits wide and are SSE registers. The rest are passed through the stack.

That is a hardware-specific implementation detail.

Recall that C has roots that date back to the 1970s, long before floating point hardware was commonplace.

It wasn't until the mid 1990s that desktop PCs had floating point chips in them. A few business machines had x87 chips, but they were relatively rare. The 486 DX chip was more expensive than the SX chip that didn't include floating point, and the SX was quite a popular consumer chip. It wasn't until 1995 or so that you could even start to assume there was FPU hardware in the x86 family.

Ubiquitous floating point is a very recent thing. Even devices people think of as modern, like the Nintendo DS, did not have floating point hardware; all those 3D graphics were done usually with fixed point math, or rarely, with software floating point libraries.

This topic is closed to new replies.

Advertisement