re: GCC Differences

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Chris,

As all have said, your missing the return.

Another mistake in my opinion is using == with floats.

You cannot depend on that to work.

This is just the nature of floating point numbers.  In your case b is probably 20.00000000000001 or something like it.

The printf is showing 20.000000 because it defaults to 6 digits of accuracy past the decimal.  Try your code with %15.15f and you will see all the digits.

Anyway, it has been a while since I have worked much with floats, so I don't remember if there is helper function / macro for this or not.

If not, you can define a simple range macro:

#define DELTA 0.000001
#define range(target, x)    ((x) < (target) - (DELTA) ? FALSE : ((x) > (target) + (DELTA) ? FALSE : TRUE))

if (range(20, f))

Greg Freemyer
Internet Engineer
Deployment and Integration Specialist
Compaq ASE - Tru64
Compaq Master ASE - SAN Architect
The Norcross Group
www.NorcrossGroup.com

>>  I'm honestly not trying to resurrect some gcc 2.96 flame war or anything 
>>  here, but I'm not a very seasoned C programmer, and I've run across an 
>>  inconsistancy between Red Hat's version of gcc, and gcc 2.95.4 on a Debian
>>  
>>  system.  Consider the following uninspired, pointless piece of code:

>>  #include <stdio.h>

>>  float toot(int, float);

>>  main() {
>>  int a = 4;
>>  float b = 5;
>>  float result = 0;

>>  result = toot(a, b);
>>  printf("%f\n", result);
>>  }

>>  float toot(int x, float y) {
>>  if (y == 20) {
>>  return y;
>>  } else {
>>  toot(x, x*y);
>>  }
>>  }


>>  Compiled with Red Hat's gcc 2.96, I get "nan" (however, If I take out 
>>  the recursive call, and just return x*y, I get 20.000000).

>>  Compiled with Debian's 2.95.4, I get 20.000000.

>>  Can anybody explain to me (a) why, and (b) if there's something inherently
>>  
>>  wrong about what I'm doing that would cause this to fail on a Red Hat 
>>  system?  I know the code is pointless, but it's an extremely dumbed down 
>>  version of a more complex problem exhibiting the exact same behavior.

>>  Thanks in advance for any insights that can be provided.  Like I said, I'm
>>  
>>  not trying to restart an old flame war, or anything -- I'm just a newbie 
>>  to C, who is honestly curious about what is going on under the covers to 
>>  cause the inconsistency.

>>  --Chris.




>>  _______________________________________________
>>  Redhat-devel-list mailing list
>>  Redhat-devel-list@redhat.com
>>  https://listman.redhat.com/mailman/listinfo/redhat-devel-list








_______________________________________________
Redhat-devel-list mailing list
Redhat-devel-list@redhat.com
https://listman.redhat.com/mailman/listinfo/redhat-devel-list

[Index of Archives]     [Kernel Newbies]     [Red Hat General]     [Fedora]     [Red Hat Install]     [Linux Kernel Development]     [Yosemite News]

  Powered by Linux