www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - ieeeFlags are not getting set.

reply "Damien" <damien.levac gmail.com> writes:
Hi everyone,

I am new to the D programming language and decided to use it for 
simple assignment. The idea is to compute the product of an 
arbitrary number of numbers and print the result in a specific 
way. The program needs to handle underflow/overflow, thus I 
decided to use ieeeFlags. The program is found below.

Basically, the flags are not getting set. Changing the the 
if-statements to check whether the resulting value is infinity or 
0 (to test respectively for overflow/underflow) and it works. So 
I am starting to think either I don't understand how this work, 
or there is a bug.

Damien

/**********/	

     import std.stdio, std.exception, std.string, std.conv, 
std.math;

     void printProduct(in float[] numberList)
     {
             float tmp = 1.0f, product = 1.0f;
             int exponent = 0;

             foreach (number; numberList) {
                     resetIeeeFlags();
                     tmp *= number; // The variable tmp is used to 
recover from errors.

                     if (ieeeFlags.overflow) {
                             writeln("lol");
                             while (product >= 1.0f) {
                                     product /= 10.0f;
                                     ++exponent;
                             }
                             product *= number; // Impossible to 
overflow anymore.
                             tmp = product;
                     } else if (ieeeFlags.underflow) {
                             while (product <= 1.0f) {
                                     product *= 10.0f;
                                     --exponent;
                             }
                             product *= number; // Impossible to 
underflow anymore.
                             tmp = product;
                     } else {
                             product = tmp;
                     }
             }

             writeln(product, " times 10 to the power ", exponent, 
".");
     }

     void main(string args[])
     {
             float[] numberList;
             foreach (number; args[1..$]) {
                     enforce(isNumeric(number), "Only numeric 
value are allowed as input.");
                     numberList ~= to!float(number);
             }

             writeln("The product is:");
             printProduct(numberList);
     }
Sep 27 2013
parent reply "Damien" <damien.levac gmail.com> writes:
I have more information.
While doing some more experiment, I noticed that at some point a 
floating-point exception was thrown.

In the documentation, it says that floating-point exception are 
disabled by default. It further says that have floating-point 
exception enabled would disable the setting of ieeeFlags. I was 
also unable to disable those exception manually.

Still hoping someone telling me how stupid I am and that is not a 
D bug (omg this language has some puns potential...)
Damien
Sep 28 2013
parent Manfred Nowak <svv1999 hotmail.com> writes:
Damien wrote:

 is not a D bug
I cannot reproduce your problem. Giving 10^38 and 10 as parameters the "lol" is output correctly. -manfred
Sep 29 2013