Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


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: 254

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.

Sponsor:

#2 rip-off   Moderators   -  Reputation: 8689

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: 2099

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: 254

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: 1640

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.

YOUR_OPINION >/dev/null

#6 SiCrane   Moderators   -  Reputation: 9662

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: 1640

Like
0Likes
Like

Posted 17 April 2014 - 10:27 AM

Ah yes, quite correct.

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


YOUR_OPINION >/dev/null

#8 L. Spiro   Crossbones+   -  Reputation: 14215

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
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#9 NightCreature83   Crossbones+   -  Reputation: 3030

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: 22693

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 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