Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   %f undefined behavior? (http://www.velocityreviews.com/forums/t315307-f-undefined-behavior.html)

Mantorok Redgormor 09-16-2003 12:51 PM

%f undefined behavior?
 
Why does the following print -1.997505? Is this because of undefined behavior?

#include <stdio.h>
int main(void)
{
printf("%f\n", 0x1f);
return 0;
}

Christopher Benson-Manica 09-16-2003 01:04 PM

Re: %f undefined behavior?
 
Mantorok Redgormor <nethlek@tokyo.com> spoke thus:
> Why does the following print -1.997505? Is this because of undefined behavior?


> #include <stdio.h>
> int main(void)
> {
> printf("%f\n", 0x1f);
> return 0;
> }


Compiling with all warnings on...
test.c:6: warning: double format, different type arg (arg 2)

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cyberspace.org |

Andreas Kahari 09-16-2003 01:11 PM

Re: %f undefined behavior?
 
In article <41ec7ac0.0309160451.1637b99@posting.google.com> ,
Mantorok Redgormor wrote:
> Why does the following print -1.997505? Is this because of undefined behavior?
>
> #include <stdio.h>
> int main(void)
> {
> printf("%f\n", 0x1f);
> return 0;
> }


Here, it prints
-14167097396369144661962308996344933237638
562294362303773503482977858904653824.000000
(except for the line break in the middle)

But then again, you're printing an integer with a floating point
format string, which makes it interpret the bit pattern of the
integer as if it was the bit pattern of a floating point value
(possibly going outside the data that was the integer).

--
Andreas Kähäri

Irrwahn Grausewitz 09-16-2003 02:12 PM

Re: %f undefined behavior?
 
Andreas Kahari <ak+usenet@freeshell.org> wrote:

>In article <41ec7ac0.0309160451.1637b99@posting.google.com> ,
>Mantorok Redgormor wrote:
>> Why does the following print -1.997505? Is this because of undefined behavior?
>>
>> #include <stdio.h>
>> int main(void)
>> {
>> printf("%f\n", 0x1f);
>> return 0;
>> }

>
>Here, it prints
>-14167097396369144661962308996344933237638
>562294362303773503482977858904653824.000000
>(except for the line break in the middle)
>
>But then again, you're printing an integer with a floating point
>format string, which makes it interpret the bit pattern of the
>integer as if it was the bit pattern of a floating point value
>(possibly going outside the data that was the integer).


Even worse: it invokes Nasal Demons, aka undefined behaviour, because
%f forces printf to retrieve an argument of sizeof(double), but there
is only sth. of size of an int available.

[OT]
IOW: this will most certainly mess up your stack, whatever that is...
[/OT]

Regards

Irrwahn
--
What does this red button do?

Peter Nilsson 09-16-2003 09:56 PM

Re: %f undefined behavior?
 
Irrwahn Grausewitz <irrwahn@freenet.de> wrote in message news:<786emvkd8qlee2cukgabfep7sil4j4hp8f@4ax.com>. ..
> Andreas Kahari <ak+usenet@freeshell.org> wrote:
>
> >In article <41ec7ac0.0309160451.1637b99@posting.google.com> ,
> >Mantorok Redgormor wrote:
> >> Why does the following print -1.997505? Is this because of undefined
> >> behavior?
> >>
> >> #include <stdio.h>
> >> int main(void)
> >> {
> >> printf("%f\n", 0x1f);
> >> return 0;
> >> }

> >
> >Here, it prints
> >-14167097396369144661962308996344933237638
> >562294362303773503482977858904653824.000000
> >(except for the line break in the middle)
> >
> >But then again, you're printing an integer with a floating point
> >format string, which makes it interpret the bit pattern of the
> >integer as if it was the bit pattern of a floating point value
> >(possibly going outside the data that was the integer).


It needn't ever even attempt to interpret the bit pattern of the
integer.

>
> Even worse: it invokes Nasal Demons, aka undefined behaviour, because
> %f forces printf to retrieve an argument of sizeof(double), but there
> is only sth. of size of an int available.


Huh? %f expects a double, not merely an object the same size of a
double.

> [OT]
> IOW: this will most certainly mess up your stack, whatever that is...
> [/OT]


Not all the world is intel.

There are implementations (modern, in use) which pass parameters via
registers, not a hardware stack.

--
Peter

Irrwahn Grausewitz 09-16-2003 11:14 PM

Re: %f undefined behavior?
 
airia@acay.com.au (Peter Nilsson) wrote:

>Irrwahn Grausewitz <irrwahn@freenet.de> wrote in message news:<786emvkd8qlee2cukgabfep7sil4j4hp8f@4ax.com>. ..
>> Andreas Kahari <ak+usenet@freeshell.org> wrote:

<SNIP>
>> >
>> >But then again, you're printing an integer with a floating point
>> >format string, which makes it interpret the bit pattern of the
>> >integer as if it was the bit pattern of a floating point value
>> >(possibly going outside the data that was the integer).

>
>It needn't ever even attempt to interpret the bit pattern of the
>integer.
>
>>
>> Even worse: it invokes Nasal Demons, aka undefined behaviour, because
>> %f forces printf to retrieve an argument of sizeof(double), but there
>> is only sth. of size of an int available.

>
>Huh? %f expects a double, not merely an object the same size of a
>double.


Hm, I used strange wording here, indeed.

>
>> [OT]
>> IOW: this will most certainly mess up your stack, whatever that is...
>> [/OT]

>
>Not all the world is intel.


Oh, really? :)

>
>There are implementations (modern, in use) which pass parameters via
>registers, not a hardware stack.


Right, and that's just the reason for [OT][/OT] tagging...
--
What does this red button do?


All times are GMT. The time now is 10:10 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.