CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2005
    Posts
    41

    Changing the exponent size

    Hi all
    I am trying to write a bunch of exponential numbers to a file. I can easily change the overall size using setw(x) and setprecision(x) to set the precision. However what I want to do is define how large the exponent is. For example, in my code it outputs an exponential number as 1.2334e+030 - what I want is to output this number as 1.2334e+30. Does anyone have any suggestions?

    thanks
    Robbie

  2. #2
    Join Date
    Nov 2008
    Posts
    2

    Re: Changing the exponent size

    might be a simple solution, but i would replace all instances of "+0" with "+"

    CString result;
    result.Replace("+00", "+0");
    result.Replace("+0", "+");

    Hope that will do the trick for you

  3. #3
    Join Date
    Apr 2005
    Posts
    41

    Re: Changing the exponent size

    Hi

    Thanks very much for the reply. Eventually I tried a different approach whereby I have written a simple function which determines what the base and exponent. That way when I write the exponential value to file then I am completely in control:

    StructDouble GetDouble(double input)
    {
    StructDouble returnval;
    returnval.val = 0;
    returnval.exp = 0;
    returnval.sign = 0;


    if(input == 0) return returnval;

    returnval.val = input;
    returnval.exp = 0;
    returnval.sign = 0;

    while(returnval.val >= 10 || returnval.val < 1)
    {
    if(returnval.val >= 10)
    {
    returnval.val /= 10.0;
    returnval.exp += 1;
    }
    else if(returnval.val < 1)
    {
    returnval.val *= 10.0;
    returnval.exp -= 1;
    }
    else
    {
    // Okay
    }
    }
    if(returnval.exp < 0)
    {
    returnval.exp *= -1;
    returnval.sign = 1;
    }

    if(returnval.exp >= 100)
    {
    returnval.val = 0;
    returnval.sign = 0;
    returnval.exp = 0;
    }

    return returnval;
    }



    where ..

    struct StructDouble
    {
    double val;
    int exp;
    int sign;
    };

    To write the exponential out, I do the following:

    ....
    StructDouble temp = GetDouble(outmat.matmatrix[i]);
    // Write value (base) - set size depending on size of exponent
    if(temp.exp < 10) outf << " " << setw(7) << setprecision(6) << temp.val << "E";
    else outf << " " << setw(6) << setprecision(5) << temp.val << "E";
    // Write the exponent sign
    if(temp.sign == 0) outf << "+";
    if(temp.sign == 1) outf << "-";
    // Finally write the exponent
    outf << temp.exp;
    ....

    I hope this might help someone eventually!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured