Jump to content

  • Log In with Google      Sign In   
  • Create Account

What's the point of floats in C?


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
9 replies to this topic

#1 gasto   Members   -  Reputation: 303

Like
-3Likes
Like

Posted 17 April 2014 - 08:44 AM

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.

#2 rip-off   Moderators   -  Reputation: 10552

Like
2Likes
Like

Posted 17 April 2014 - 08:51 AM

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



#3 nobodynews   Crossbones+   -  Reputation: 2999

Like
4Likes
Like

Posted 17 April 2014 - 09:34 AM

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!


#4 gasto   Members   -  Reputation: 303

Like
5Likes
Like

Posted 17 April 2014 - 09:44 AM

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.

#5 TheComet   Crossbones+   -  Reputation: 2536

Like
0Likes
Like

Posted 17 April 2014 - 09:59 AM

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, 17 April 2014 - 10:01 AM.

"Windows 10 doesn't only include spyware, it is designed as spyware" -- Gaius Publius, The Big Picture RT Interview

"[...] we will access, disclose and preserve personal data, including your content (such as the content of your emails, other private communications or files in private folders), when we have a good faith belief that doing so is necessary" -- Windows 10 Privacy Statement


#6 SiCrane   Moderators   -  Reputation: 11320

Like
0Likes
Like

Posted 17 April 2014 - 10:19 AM

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



#7 TheComet   Crossbones+   -  Reputation: 2536

Like
0Likes
Like

Posted 17 April 2014 - 10:27 AM

Ah yes, quite correct.

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


"Windows 10 doesn't only include spyware, it is designed as spyware" -- Gaius Publius, The Big Picture RT Interview

"[...] we will access, disclose and preserve personal data, including your content (such as the content of your emails, other private communications or files in private folders), when we have a good faith belief that doing so is necessary" -- Windows 10 Privacy Statement


#8 L. Spiro   Crossbones+   -  Reputation: 24028

Like
5Likes
Like

Posted 17 April 2014 - 10:33 AM

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

#9 NightCreature83   Crossbones+   -  Reputation: 4644

Like
-1Likes
Like

Posted 17 April 2014 - 11:05 AM

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, Mad Max

#10 frob   Moderators   -  Reputation: 39124

Like
3Likes
Like

Posted 17 April 2014 - 03:02 PM

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.


Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.





Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.




PARTNERS