Discussion:
gcc option: about -fsignaling nans : how does it work ?
(too old to reply)
r***@yahoo.com
2005-01-16 05:50:36 UTC
Permalink
Let's say I have this sample code fragment.

#include <stdio.h>
#include <stdlib.h>

int main() {
double dim = 0.0;
double invdim;
invdim = 0.0/(double)dim; // NaN is produced here.
invdim += 0.1; // I want to catch Nan before performing
//any arithmetic operations on it.
//How do I do it ?

printf("invdim: %lf\n", invdim);
return EXIT_SUCCESS;
}


I want to catch the Nan value before I do any arithmetic operations on
the same. How would I do it ?
From the ANSI spec 5.2.4.2.2 Characteristics of floating types
<float.h>

<--
"A quiet NaN propagates through almost every arithmetic operation
without raising a floating-point exception; a signaling NaN generally
raises a floating-point exception when occurring as an arithmetic
operand."
-->

$ gcc -Wall -fsignaling-nans fp_test.c

When I ran the program, my program went past the nan creation point and
printed the value.

invdim: nan

But I expected an exception / crash at the point when -fnan is
specified. What am i missing here. thanks for letting me know.
Larry I Smith
2005-01-16 16:48:59 UTC
Permalink
Post by r***@yahoo.com
Let's say I have this sample code fragment.
#include <stdio.h>
#include <stdlib.h>
int main() {
double dim = 0.0;
double invdim;
invdim = 0.0/(double)dim; // NaN is produced here.
invdim += 0.1; // I want to catch Nan before performing
//any arithmetic operations on it.
//How do I do it ?
printf("invdim: %lf\n", invdim);
return EXIT_SUCCESS;
}
I want to catch the Nan value before I do any arithmetic operations on
the same. How would I do it ?
From the ANSI spec 5.2.4.2.2 Characteristics of floating types
<float.h>
<--
"A quiet NaN propagates through almost every arithmetic operation
without raising a floating-point exception; a signaling NaN generally
raises a floating-point exception when occurring as an arithmetic
operand."
-->
$ gcc -Wall -fsignaling-nans fp_test.c
When I ran the program, my program went past the nan creation point and
printed the value.
invdim: nan
But I expected an exception / crash at the point when -fnan is
specified. What am i missing here. thanks for letting me know.
From the CGG 'info' pages:

`-fsignaling-nans'
Compile code assuming that IEEE signaling NaNs may generate
user-visible traps during floating-point operations. Setting this
option disables optimizations that may change the number of
exceptions visible with signaling NaNs. This option implies
`-ftrapping-math'.


This option causes the preprocessor macro `__SUPPORT_SNAN__' to be
defined.


The default is `-fno-signaling-nans'.


This option is experimental and does not currently guarantee to
disable all GCC optimizations that affect signaling NaN behavior.


The above explanation is a little unclear to me, but it appears to
affect only the compiler's optimization features (it's found on the
'Optimize Options' manual page). It does not seem that the option
causes any signals or exceptions to be generated.

Regards,
Larry
--
Anti-spam address, change each 'X' to '.' to reply directly.
r***@yahoo.com
2005-01-16 23:06:15 UTC
Permalink
Post by Larry I Smith
Post by r***@yahoo.com
Let's say I have this sample code fragment.
#include <stdio.h>
#include <stdlib.h>
int main() {
double dim = 0.0;
double invdim;
invdim = 0.0/(double)dim; // NaN is produced here.
invdim += 0.1; // I want to catch Nan before performing
//any arithmetic operations on it.
//How do I do it ?
printf("invdim: %lf\n", invdim);
return EXIT_SUCCESS;
}
I want to catch the Nan value before I do any arithmetic operations on
the same. How would I do it ?
From the ANSI spec 5.2.4.2.2 Characteristics of floating types
<float.h>
<--
"A quiet NaN propagates through almost every arithmetic operation
without raising a floating-point exception; a signaling NaN
generally
Post by Larry I Smith
Post by r***@yahoo.com
raises a floating-point exception when occurring as an arithmetic
operand."
-->
$ gcc -Wall -fsignaling-nans fp_test.c
When I ran the program, my program went past the nan creation point and
printed the value.
invdim: nan
But I expected an exception / crash at the point when -fnan is
specified. What am i missing here. thanks for letting me know.
`-fsignaling-nans'
Compile code assuming that IEEE signaling NaNs may generate
user-visible traps during floating-point operations. Setting this
option disables optimizations that may change the number of
exceptions visible with signaling NaNs. This option implies
`-ftrapping-math'.
This option causes the preprocessor macro `__SUPPORT_SNAN__' to be
defined.
The default is `-fno-signaling-nans'.
This option is experimental and does not currently guarantee to
disable all GCC optimizations that affect signaling NaN
behavior.
Post by Larry I Smith
The above explanation is a little unclear to me, but it appears to
affect only the compiler's optimization features (it's found on the
'Optimize Options' manual page). It does not seem that the option
causes any signals or exceptions to be generated.
Yeah. But the C standard says -
"a signaling NaN generally raises a floating-point exception when
occurring as an arithmetic operand." .

I think GNU C compiler confirms to the ANSI C standard ( at least
until before c99 standard ). I googled for the same, but continued to
get copies of manuals for the same.
Larry I Smith
2005-01-17 00:42:25 UTC
Permalink
Post by r***@yahoo.com
Post by Larry I Smith
Post by r***@yahoo.com
Let's say I have this sample code fragment.
#include <stdio.h>
#include <stdlib.h>
int main() {
double dim = 0.0;
double invdim;
invdim = 0.0/(double)dim; // NaN is produced here.
invdim += 0.1; // I want to catch Nan before performing
//any arithmetic operations on it.
//How do I do it ?
printf("invdim: %lf\n", invdim);
return EXIT_SUCCESS;
}
I want to catch the Nan value before I do any arithmetic operations
on
Post by Larry I Smith
Post by r***@yahoo.com
the same. How would I do it ?
From the ANSI spec 5.2.4.2.2 Characteristics of floating types
<float.h>
<--
"A quiet NaN propagates through almost every arithmetic operation
without raising a floating-point exception; a signaling NaN
generally
Post by Larry I Smith
Post by r***@yahoo.com
raises a floating-point exception when occurring as an arithmetic
operand."
-->
$ gcc -Wall -fsignaling-nans fp_test.c
When I ran the program, my program went past the nan creation point
and
Post by Larry I Smith
Post by r***@yahoo.com
printed the value.
invdim: nan
But I expected an exception / crash at the point when -fnan is
specified. What am i missing here. thanks for letting me know.
`-fsignaling-nans'
Compile code assuming that IEEE signaling NaNs may generate
user-visible traps during floating-point operations. Setting
this
Post by Larry I Smith
option disables optimizations that may change the number of
exceptions visible with signaling NaNs. This option implies
`-ftrapping-math'.
This option causes the preprocessor macro `__SUPPORT_SNAN__'
to be
Post by Larry I Smith
defined.
The default is `-fno-signaling-nans'.
This option is experimental and does not currently guarantee
to
Post by Larry I Smith
disable all GCC optimizations that affect signaling NaN
behavior.
Post by Larry I Smith
The above explanation is a little unclear to me, but it appears to
affect only the compiler's optimization features (it's found on the
'Optimize Options' manual page). It does not seem that the option
causes any signals or exceptions to be generated.
Yeah. But the C standard says -
"a signaling NaN generally raises a floating-point exception when
occurring as an arithmetic operand." .
I think GNU C compiler confirms to the ANSI C standard ( at least
until before c99 standard ). I googled for the same, but continued to
get copies of manuals for the same.
I believe that it's up to the FPU (hardware)
to gen the NaNs; all the compiler switch does
is enable support if one occurs.

Regards,
Larry
--
Anti-spam address, change each 'X' to '.' to reply directly.
Norbert Juffa
2005-01-16 23:35:20 UTC
Permalink
This post might be inappropriate. Click to display it.
Loading...