Conversion of float in if statement
NickName:Slaven Glumac Ask DateTime:2013-02-21T18:29:39

Conversion of float in if statement

In following code:

float x = 0.23;
int z;

z = x;

if (x)
    printf("float %f will not be converted to 0!\n", x);

if (z)
    printf("this will not print!\n");

I thought that floating point number will be converted to integer and then checked if it is zero when used in if statement. Can someone please explain to me, why that is not the case?

edit:

(just to clarify what has been confusing me) Since logical operators like < return int, I thought that if statement receives integer value so conversion needs to be done. As pointed out by answers, it doesn't. Thank you everyone!

Copyright Notice:Content Author:「Slaven Glumac」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/14999940/conversion-of-float-in-if-statement

Answers
Mike Seymour 2013-02-21T10:58:38

\n I thought that floating point number will be converted to integer and then checked if it is zero when used in if statement.\n\n\nNo, it will just be compared with zero. The rule for converting arithmetic types to boolean is given in C++11 4.12:\n\n\n A zero value [...] is converted to false;\n any other value is converted to true.\n",


Hewei Liu 2013-02-21T11:13:22

\ntype converting happens when operating with other types, and the integer will be converted to float when operating between them, for example: x + z // z will be converted to \"float\" before add with \"x\"\nif used as an logical condition in \"if()\", it is only judged that \"null/zero or not\". 0.23 is not zero/null, so the result is TRUE.\n",


Azodious 2013-02-21T10:31:58

Compiler will only check if the argument in if is zero. if not, the condition is true. No conversion is done at all.",


unwind 2013-02-21T10:32:48

Your first test is testing x, the float, so no \"conversion\" happens there.\n\nBasically, the if will check if the expression is 0 or non-zero, and clearly 0.23 is non-zero so that code should execute.\n\nYou can think of\n\nif(x)\n\n\nas being the same as\n\nif(x != 0)\n",


More about “Conversion of float in if statement” related questions

Conversion of float in if statement

In following code: float x = 0.23; int z; z = x; if (x) printf("float %f will not be converted to 0!\n", x); if (z) printf("this will not print!\n"); I thought that float

Show Detail

Safe conversion of Float to Int?

Float to Int conversion is documented to be a simple as: let i = Int(x) However this alone is unsafe, as a Swift app will crash if x is too big to fit in an integer, or is a NaN of some kind. So...

Show Detail

Conversion of string value in an array to float in php

Hello friends I have two arrays called var $conversion_rates; and var $LastUpdate_rates; now I am updating them in my class with values . If you do var_dump() both of them you get values lik...

Show Detail

Migration conversion of float data to decimal data

I am migrating data from one table to a new table. The old table uses FLOAT, and in the new table I am using DECIMAL as the field attribute. I am using the following statement which worked fine: C...

Show Detail

int to float conversion produces a warning?

It's surprising for me to see that even when the value can be converted, an int to float conversion always give a warning. Why is this? int i = 0; float f = 0; // warning here // I thought this ...

Show Detail

Float to Int vs Int to Float automatic conversion

I'm studying java by Herbert Schildt's "Java for beginners" book. It is said that, being a destiny variable compatible and sufficiently big to store an origin one, an automatic conversion is done. ...

Show Detail

ASP.Net conversion on using float numbers

I have a conversion or some kind of type casting problem in my asp.net/C# math app. A contractor wrote some code to calculate a relative standard error in an assembly. But when I run it, apparently

Show Detail

Whether an implicit type conversion occurs between decimal literals and float types

In C++,the type of floating-point literals is double by default; auto dval = 3.14; // dval is a double So,in the statement float fval = 3.14 , 3.14 -&gt; float means double -&gt; float? Another

Show Detail

Estimate or bound the float conversion error in Python

I am reading data from a csv file that contain numbers, and convert them to numpy float32 / float64. I'd like to get an estimation (or a bound) of the error that I have when converting. Ideally I'd...

Show Detail

Float to byte array conversion

We use float f= 3.5f ; BitConverter.GetBytes(f); It returns byte array of length 4. But I wonder the math behind this. Anyone will teach the math behind this conversion? Also double conversion i...

Show Detail