Saturday, 22 March 2014

Re: [gccsdk] [Bug 250] New: GCC 4.7.4 Rel 1 Dev 2014-01-08: sqrt function

Hello Duncan,

In message <bug-250-73@http.www.riscos.info/bugzilla3/> you wrote:

> printf("%f\n",sqrt(2.0));

Looks like you have fallen in a classic C pitfall:
passing a double argument to a vararg/stdarg function
but interpreting it as float.

Try either:
printf("%lf\n", sqrt(2.0));
Or:
printf("%f\n", (float) sqrt(2.0));

Reason: float is a four byte type, double is 8 bytes.
And even if it you'd use sqrtf() or fsqrt() (these
non-standard functions are not present in the RISC OS
version of the library as far as I know), standard C
would still promote the float argument to a double
before passing control to printf().

That in turn is presented with 8 bytes but the float
format only pulls 4, and interprets that as float. So
any output would be possible.

(You may want to have a look at Andrew Koenig's book
`C Traps and Pitfalls'. It was already an ancient book
when I was a student, but the traps and pitfalls haven't
changed since.)

Regards,

Bob.

--
Bob Brand

_______________________________________________
GCCSDK mailing list gcc@gccsdk.riscos.info
Bugzilla: http://www.riscos.info/bugzilla/index.cgi
List Info: http://www.riscos.info/mailman/listinfo/gcc
Main Page: http://www.riscos.info/index.php/GCCSDK

No comments:

Post a Comment