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

## Recommended Posts

This is a very fast way to cast an integer to a float. I use it extensively in my projects.

int ix = 58;

float fx = ix + 0.0;

##### Share on other sites

Faster, as in less typing required :D

##### Share on other sites
I do something similar to convert user input to numeric in php:

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

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

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5

• 12
• 19
• 10
• 14
• 10
• ### Forum Statistics

• Total Topics
632664
• Total Posts
3007708
• ### Who's Online (See full list)

There are no registered users currently online

×