float x = 1.33333333333333 why does it equal 1?

Started by
15 comments, last by alnite 19 years, 6 months ago
Hi, I have made a float like so float x = 400/300 400/300 = 1.333333333333333333333333333333333 When i find out what value x is like so this.text = x.tostring(); text = 1 for some reason Do recurring numbers get rounded off if so, how can i keep it so x = 1.3333? Thanks for any info
Advertisement
because you are doing an integer division.

float x = (float)400/300


That will do what you want.
If both numbers on either side of a division are integers a ‘integer division’ is performed. This is basicly a division that is trunctuated to an integer (for the sake of speed). To solve this you can cast the values as floats or doubles.

Example:

Floats:
400.0f / 300.0f

Floats:
( float ) 400 / (float ) 300

Doubles:
400.0 / 300.0

Hope this helps,
Jackson Allan
Quote:Original post by Xetrov
because you are doing an integer division.

float x = (float)400/300




Casting literals is strange. Why not this?

    float x = 400.0f / 300.0f 

John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Quote:
Casting literals is strange. Why not this?

    float x = 400.0f / 300.0f 


Fair enough, but that doesnt exactly work when you want to use variables instead ;)
Quote:Original post by Xetrov
Fair enough, but that doesnt exactly work when you want to use variables instead ;)


(Kicking the man while he's down.)
C casts are evil (unless you're programimng in pure C).
Prefer static_cast<float>(a)/b.
That way it is obvious what the cast is applied to.
(does the cast in (float)a/b apply to a or to a/b ?)

[wink]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
I see :)
Thanks everyone :)
Quote:Original post by Fruny
Prefer static_cast<float>(a)/b.
That way it is obvious what the cast is applied to.
(does the cast in (float)a/b apply to a or to a/b ?)

Although C-casts are ugly, I usually find this nice:
float(a)/b
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
That's not a C cast, you're creating a temporary float from a. I guess in the end it's exactly the same, but I'm going to say they're different, so har!
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Quote:Original post by dalleboy
Quote:Original post by Fruny
Prefer static_cast<float>(a)/b.
That way it is obvious what the cast is applied to.
(does the cast in (float)a/b apply to a or to a/b ?)

Although C-casts are ugly, I usually find this nice:
float(a)/b


float x = (float)(a/b) is the way I did

This topic is closed to new replies.

Advertisement