1

I'm a Little confused about some numbers in C-Code. I have the following piece of Code

int k;
float a = 0.04f;

for (k=0; k*a < 0.12; k++) {
  * do something *
}

in this case, what is the type of "0.12" ? double ? float ? it has never been declared anywhere. Anyways, in my program the Loop above is excuted 4 times, even though 3*0.04=0.12 < 0.12 is not true. Once I Exchange 0.12 with 0.12F (because I am globally restricted to float precision in all of the program), the Loop is now executed 3 times. I do not understand why, and what is happening here. Are there any proper Guidelines on how to write such statements to not get unexpected issues?

Another related issue is the following: in the Definition of variables, say

float b = 1/180 * 3.14159265359;

what exactly "is" "1" in this case ? and "180" ? Integers ? Are they converted to float numbers ? Is it okay to write it like that ? Or should it be "1.0f/180.0f*3.14159265359f;"

Last part of the question,

if i have a fuction

void testfunction(float a)

which does some things.

if I call the fuction with testfunction(40.0/6.0), how is that Division handled ? It seems that its calculated with double precision and then converted to a float. Why ?

That was a Long question, I hope someone can help me understand it.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
mch
  • 105
  • 1
  • You should try your angle answer and see what result you get for b when you use intergers versus floats. You're asking about number literals. – matt Dec 17 '18 at 12:26
  • Types of numeric constants are described in section 6.4.4. Conversions are in 6.3. [C11 standard draft](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf) I'd recommend trying to read it; I found it to be not nearly as scary as I expected. –  Dec 17 '18 at 12:28
  • There are 3 distinct questions, not one. – Antti Haapala -- Слава Україні Dec 17 '18 at 12:41

3 Answers3

3

...numbers that are not stored in a variable

They are called "constants".

  • Any unsuffixed floating point constant has type double.

    Quoting C11, chapter §6.4.4.2

    An unsuffixed floating constant has type double. If suffixed by the letter f or F, it has type float. If suffixed by the letter l or L, it has type long double.

  • For Integer constants, the type will depend on the value.

    Quoting C11, chapter §6.4.4.1,

    The type of an integer constant is the first of the corresponding list in which its value can be represented. [..]

    and for unsuffixed decimal number, the list is

    int
    long int
    long long int
    

Regarding the mathematical operation accuracy of floating point numbers, see this post

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

"0.12" is a constant, and yes, without a trailing f or F, it will be interpreted as a double.

That number can not be expressed exactly as a binary fraction. When you compare k * a, the result is a float because both operands are floats. The result is slightly less than 0.12, but when you compare against a double, it gets padded out with zeros to the required size, which increases the discrepancy. When you use a float constant, the result is not padded (cast), and by good luck, comes out exactly equal to the binary representation of "0.12f". This would likely not be the case for a more complicated operation.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
0

if we use a fractional number for eg, like 3.4 it's default type if double. You should explicitly specify it like 3.4f if you want it to consider as single precision (float). if you call the following function

int fun()
{
    float a= 1.2f;
    double b = 1.2;

    return (a==b);
}

this will always return zero (false).because before making comparison the float type is converted to double (lower type to higher type) , At this time sometimes it can't reproduce exact 1.2, a slight variations in value can be happen after 6th position from decimal point. You can check the difference by the following print statement

printf("double: %0.9f \n float: %0.9f\n",1.2,1.2f);

It results ike

double: 1.200000000
float: 1.200000481