Jump to content
  • Advertisement
Sign in to follow this  
DedicatedGamer

Fast way to cast int to float!

This topic is 766 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

Advertisement
I do something similar to convert user input to numeric in php:

$safer = $_GET['ANYTHING'] + 0
:lol:

Share this post


Link to post
Share on other sites

The sad thing is that fx = ix + 0.0f is actually different from fx = ix unless you're using something like -ffast-math (compilers can't optimize floating point operations because that can introduce different rounding errors and hence technically cause the result to be different, which is not allowed by the standard).

Share this post


Link to post
Share on other sites

I actually do not know if this is actually faster, it just seems like it would be, because it is a single call as opposed to passing a variable to a function (proper typcasting).

 

int ix = 58;

float = (int) ix;

 

Also, I don't think it gives a compiler warning. I also do this a lot in my projects:

 

float fx = 1.983;

int ix = fx;

 

Which DOES produce a very nasty warning, so I disabled the warning for that specifically. :)

Share this post


Link to post
Share on other sites
Typecasting isn't always a function.

For static casts and C style casts it's done purely as a compile time operation or turned into a copy operation, you're telling the compiler "these four bytes that I said were an int? I was lying. They're actually a float, treat them as such and don't question me".

Of course for dynamic casts and certain types of conversion the runtime does checks and balances but these are both internal and inline as far as you're concerned.

The best thing to do is avoid static casts where you can, not because of speed but because you're essentially saying "hi compiler, please discard data". In the end if you want to fit your square peg in a round hole you're going to have to trim it to fit...

Share this post


Link to post
Share on other sites


If you mean to cast, use the language of a cast -- otherwise it obscures your intent and makes the job of understanding what you mean to happen harder for both humans and the compiler.
Ah, you must be using one of those strongly typed languages, or a language where type rules are evaluated at compile time.

 

Javascript is the current darling at many companies, where HTML5 is everywhere. Weakly typed with dynamic type evaluation means evil things. Casting is automatic and is done behind your back. Dynamic casting (also called 'duck typing') coupled with automatic casting rules mean that whatever your data actually holds, either the system will find some terrible alternate type conversion that makes no sense to the programmer but is valid for the browser, or it will fail silently.  Why is the page blank on this browser but works perfectly on three others? Nobody knows, and good luck getting the browser to tell you!

 

Python is also a mainstay of tools developers. Some systems work with it, but for most, compile-time evaluation is a dream. It is only at runtime you are allowed to discover if you provided all the functionality for duck typing. Any changes to any upstream library and suddenly you discover you need to add many new functions. But you cannot be told at compile time, only after it crashes when QA is abusing it.  And heaven help you if your logging system doesn't immediately reveal what functions were expected but missing.

 

 

In somewhat related news, today a coworker said two words that were never meant to go together: "Enterprise JavaScript".  With systems like node.js being more popular we are seeing the rise of abominations that should have been killed on sight, without mercy.

 

I told him I did not accept "Enterprise JavaScript" could possibly be a thing in a reality I participate in.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!