float() function

J

jan olieslagers

Given following code, how to understand the resulting output?
The integer version works, but a straightforward eqivalent for floats or
double produces gibberish (or what must I call the "nan"?)
Seems like the non-integer parameters never get entered into the
functions, what am i missing?
NB this is with gcc 4.2.1 on SuSe 10.3 Linux.
Thanks in advance, and sincere apologies if I _still_ haven't done
enough RTFM!

<code>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int test_function(input_int)
int input_int;
{
printf(" C1: %d \n",input_int);
return (input_int -2);
}

int test_float(input_float)
float input_float;
{
printf(" C2: %f \n",input_float);
return (input_float -2);
}

int test_double(input_double)
double input_double;
{
printf(" C3: %f \n",input_double);
return (input_double -2);
}

main (){
printf("%d %d\n",3,test_function(3));
printf("%d %d\n",5,test_function(5));
printf("%f %f\n",3,test_float(3));
printf("%f %f\n",5,test_float(5));
printf("%f %f\n",3,test_double(3));
printf("%f %f\n",5,test_double(5));
}

</code>

<output>
C1: 3
3 1
C1: 5
5 3
C2: -1.998314
nan 2.399940
C2: -1.998314
nan 2.399940
C3: -1.998314
nan 2.399940
C3: -1.998314
nan 2.399940
</output>
 
K

Keith Thompson

jan olieslagers said:
Given following code, how to understand the resulting output?
The integer version works, but a straightforward eqivalent for floats
or double produces gibberish (or what must I call the "nan"?)
Seems like the non-integer parameters never get entered into the
functions, what am i missing?
NB this is with gcc 4.2.1 on SuSe 10.3 Linux.
Thanks in advance, and sincere apologies if I _still_ haven't done
enough RTFM!
<code>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int test_function(input_int)
int input_int;
{
printf(" C1: %d \n",input_int);
return (input_int -2);
}

int test_float(input_float)
float input_float;
{
printf(" C2: %f \n",input_float);
return (input_float -2);
}

int test_double(input_double)
double input_double;
{
printf(" C3: %f \n",input_double);
return (input_double -2);
}

main (){
printf("%d %d\n",3,test_function(3));
printf("%d %d\n",5,test_function(5));
printf("%f %f\n",3,test_float(3));
printf("%f %f\n",5,test_float(5));
printf("%f %f\n",3,test_double(3));
printf("%f %f\n",5,test_double(5));
}

</code>

Your code would be a lot easier to read if it were indented properly.

You don't use anything in <math.h> or <stdlib.h>, so you can drop two
of your #include directives.

All your functions use old-style declarations. There's no good reason
not to use prototypes. Without prototypes, when the compiler sees a
call to a function, it doesn't know what number and type(s) of
arguments it expects. So when you write "test_double(3)", the
compiler generates code to pass the int value 3 (which is garbage or
worse when interpreted as a double). If you had a prototype, letting
the compiler know that test_double expects a double argument, the
compiler would implicitly convert the int value 3 to the double value
3.0.

Similarly, you pass int arguments to printf corresponding to "%f",
which tells printf to expect a double argument (possibly promoted from
float). Since printf takes a variable number and types of arguments,
you can't declare a prototype that tells the compiler what to pass it,
so it's up to you to pass the correct number and types of arguments.

Your test_float and test_double functions return int results. I'm
going to guess that you wanted them to return float and double,
respectively.

Here's a corrected version of your program. I've made a few stylistic
improvements as well.

#include <stdio.h>

int test_function(int input_int)
{
printf(" C1: %d\n",input_int);
return input_int - 2;
}

float test_float(float input_float)
{
printf(" C2: %f\n",input_float);
return input_float - 2;
}

double test_double(double input_double)
{
printf(" C3: %f\n",input_double);
return input_double - 2;
}

int main(void) {
printf("%d %d\n", 3, test_function(3));
printf("%d %d\n", 5, test_function(5));
printf("%f %f\n", 3.0, test_float(3));
printf("%f %f\n", 5.0, test_float(5));
printf("%f %f\n", 3.0, test_double(3));
printf("%f %f\n", 5.0, test_double(5));
return 0;
}
 
M

Martin Ambuhl

jan said:
Given following code, how to understand the resulting output?
The integer version works, but a straightforward eqivalent for floats or
double produces gibberish (or what must I call the "nan"?)


/* I really can't tell what your question is, but here is a version of
your code which conforms more closely to the modern idiom of C. It
also fixes your eight (8) errors in the specifiers for printf.
Those errors lead to the output you cannot understand. */
#include <stdio.h>
#if 0
#include <stdlib.h>
#include <math.h>
#endif

int test_function(int input_int)
{
printf(" C1: %d \n", input_int);
return input_int - 2;
}

int test_float(float input_float)
{
printf(" C2: %f \n", input_float);
return input_float - 2;
}

int test_double(double input_double)
{
printf(" C3: %f \n", input_double);
return input_double - 2;
}

/* main returns an int: you should say so. */
int main()
{
printf("%d %d\n", 3, test_function(3));
printf("%d %d\n", 5, test_function(5));
printf("%d %d\n", 3, test_float(3));
printf("%d %d\n", 5, test_float(5));
printf("%d %d\n", 3, test_double(3));
printf("%d %d\n", 5, test_double(5));
/* functions that return values should do so. Your use of implicit
int for main marks your code as pre-C99, but leaving out the
return value from main marks it as C99 or later. */
return 0;
}
 
J

jan olieslagers

jan olieslagers schreef:
Given following code, how to understand the resulting output?
The integer version works, but a straightforward eqivalent for floats or
double produces gibberish (or what must I call the "nan"?)

OK gentlemen, all sorted out now, my gratitude! The best lesson is the
use of the -W options to gcc so as to get some clearer diagnostics.

KA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top