• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

Problem returning float from function

I have a function that converts a char array to a float and then returns the float (thanks again to sunnycoder).  For whatever reason the function is returning some strange number.  Here's my code:

float returnFloatVar(){
//rply.value is a char array in a struct

float t = atof(rply.value);   //convert char array to float
printf("value: %f\n",t);  //print out float value.  correctly prints out value of 50.5
return t;
}

getVal(){
float p = returnFloatVar();
printf("value: %f\n",p);    //prints out 134523168.00000
}

I'm probably doing something stupid, but I can't see it.

0
ChrisClement
Asked:
ChrisClement
  • 5
  • 3
  • 3
  • +5
1 Solution
 
Jaime OlivaresCommented:
in fact atof returns a double.
and printf("....%f....") prints a double, not a float.
So, maybe you an use doubles instead of floats.
0
 
Jaime OlivaresCommented:
Sorry, typo, my keyboard is dirty, I think. I meant:

Maybe you can use doubles instead of floats.
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi ChrisClement,

I've run this snippet (after adding main() and a couple of includes) and it works fine.

Do you include the stdlib.h and stdio.h files?


Good Luck!
Kent
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ChrisClementAuthor Commented:
Kdo,

Yeah, I did.  The function that returns the float is in a different c file and both files have those libraries.  I tried a simple test program and it worked fine.  I can't figure out why it does not return the correct value in my app though.

Chris
0
 
PaulCaswellCommented:
Hi ChrisClement,

The code you've posted, as it is, cannot possibly do what you say! :-)

However, I am sure there is something between the 'return t' and the 'float p=' that is trashing the value. Could you tell us more about the linkage between the two? Is one in a DLL for example?

Paul
0
 
ChrisClementAuthor Commented:
Paul,

Yeah, I know.  There's something between the return and the assignment that just ain't jiving.  Here's how the two are related.

First, this is on PC-BSD and I'm compiling using cc.  The return function is in a file called dsmc-stub.c.  The code that assigns it to a float is in a file called dsmc.c.  I have several other functions in dsmc-stub.c and all of them return the correct value except this one.  dsmc-stub.o is linked with dsmc with the following command.

cc-I -04 -c dsmc.c
cc -I -04 -c dsmc-stub.c
cc -I -04 dsmc.o connectTCP.o connectsock.o errexit.o dsmc-stub.o -o dsmc

This is for a Unix network programming class and some of the code is the professor's, not mine, but this is such a simple problem and has nothing to do with the goal of the assignment (except that it is causing mysterious problems) so answering will not be doing my work for me.  :-)  But for the life of me I can't seem to figure out what the heck is going on.  It's a simple function return!!!

Chris
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi ChrisClement,

Do I understand this correctly?  One of the functions in the example is in a library provided by the class?  If so, it could be a simple mismatch between compilers/options.


Kent
0
 
ChrisClementAuthor Commented:
Kent,

Ok, but if so wouldn't that affect all my functions?  I have some that return ints and chars as well, but they aren't having problems.  Just the functions that return floats.

Chris
0
 
ChrisClementAuthor Commented:
It gets weirder.  I put a simple function in my dsmc-stub file that does ONE thing: return 56.65;  But it returns 24.00000.  

I'm confused.
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi ChrisClement,
> Ok, but if so wouldn't that affect all my functions?

Not necessarily.  Depending on the age of the compiler, host system and a few other things, the module in the library could be from an old library that is returning a 32 bit float instead of a 64 bit.  The IEEE 754 floating point standard (the standard that your program seems to adhere to) may not be the foundation for the floating point in the library.


Kent
0
 
PaulCaswellCommented:
Hi ChrisClement,

I think Kents idea is better than all of my initial ones. If it turns out to be wrong (and I doubt that) here's some other possibilities.

Are you blowing the stack?

Have you got any possibly misbehaving interrupts running?

The fact that you are using  a stub suggests some form of communication is going on between these two components. Is there some other potentially faulty process between the two?

Paul
0
 
PaulCaswellCommented:
Hi ChrisClement,

The existence of connectTCP.o connectsock.o in the build is a sign of long-distance network communication. Is there any chance that the return value passes through one of these protocols?

They may be interrupt-driven, perhaps they are clobbering your stack.

Paul
0
 
sunnycoderCommented:
I would suggest that first thing you do is use doubles instead of floats ....

Next, compile with -Wall flag and see if there are some warnings that could be potential source of the problem.

Last, show us the exact definition (copy paste them without leaving out anything) of the two functions involved.
0
 
brettmjohnsonCommented:
I suspect you are not properly declaring returnFloatVar() in dsmc.c.
Without a proper function prototype for returnFloatVar, the caller
will assume it returns an int.  When you say all the other functions
in the stub return the correct value, I suspect they were either properly
prototyped, or they return an int (or a datatype that is the same
size as int).  As sunnycode suggests, compile with full warnings (-Wall)
an you should see warnings about missing prototypes or coerced
return values.
0
 
NopiusCommented:
I suppose you should define 'extern rply' if rply is defined in another C source file.
extern struct what_is_your_type rply;
0
 
wnrossCommented:
Whew, you sur esent everyone on a wild goose chase today, must be the end of the week

The problem here is a mismatched prototype: try adding

#include <stdlib.h>

To the beginning of your file, things should clear up nicely

Cheers
-Bill
0
 
wnrossCommented:
^^^^ you sure sent  ^^^^
0
 
ChrisClementAuthor Commented:
Thanks, everyone, for the help.  brettmjohnson, you hit it dead on.  I declared my functions in my stub file but not in the other file where they were being called.      Thanks again.  
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 3
  • 3
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now