• Advertisement
Sign in to follow this  

What's the point of floats in C?

This topic is 1376 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites

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;
Edited by TheComet

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement