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);
    }

Reply via email to