4

A number 170 can be represented as a normalised form with:
1,7 as the fractional coefficient
+2 as the exponent with 10 as the base.

170 = 1,7 × 10+2

Is there a way to extract the "fractional coefficient" in a mathematical way? Without using the string representation.

var testInputs = 
    new double[] {
        1.7E+1,     // 17
        1.7E+2,     // 170
        1.7E+3,     // 1700

        -1.7E+1,    // -17
        -1.7E+2,    // -170
        -1.7E+3,    // -1700

        1.7E-1,     // 0,17
        1.7E-2,     // 0,017
        1.7E-3,     // 0,0017

        -1.7E-1,    // -0,17
        -1.7E-2,    // -0,017
        -1.7E-3,    // -0,0017
    };

The absolute value of the fractional coefficient of those test is : 1,7.

Nb: , is the decimal separator.

xdtTransform
  • 1,986
  • 14
  • 34
  • Disclaimer not Homework, I was asking my self if there was a bitwise operation that give result better that somethign like `string GetFractional(double input) => input.ToString("E").Substring(0, input.ToString("E").IndexOf("E")).TrimEnd('0');` not tested. – xdtTransform Mar 04 '19 at 15:07
  • Can you add the expected outputs to your `testInputs`? – canton7 Mar 04 '19 at 15:08
  • how about If x > 0 divide by 10 until the value becomes smaller then 0.... – Mong Zhu Mar 04 '19 at 15:09
  • Note that your array here isn't what you expect. The commas are separating the numbers into two distinct values. – DavidG Mar 04 '19 at 15:09
  • @canton7, "The absolute value of the fractional coefficient of those test is : 1,7." the result is either 1.7 or -1.7 the part without the E – xdtTransform Mar 04 '19 at 15:09
  • @DavidG, nice catch my bad Editing. – xdtTransform Mar 04 '19 at 15:10
  • 1
    [This question](https://stackoverflow.com/questions/389993/extracting-mantissa-and-exponent-from-double-in-c-sharp) may help you – John M Mar 04 '19 at 15:11
  • @xdtTransform If you not really care about accuracy you can use logarithm `var log = Math.Log10(1700); var res = Math.Pow(10, log - Math.Truncate(log));` – Aleks Andreev Mar 04 '19 at 15:15
  • @JohnM, while this seems revelant and it's from Skeet, the function return Nothing and the value of those variable in debug looked wrong. For -1.7E-3, I got -62 and 7839866231326559. – xdtTransform Mar 04 '19 at 15:31

3 Answers3

5

EDIT: initial version didn't work for (-1<x<1)

If I get your query right...

var X = -1.7; 
var Y = (decimal)X/(decimal)(Math.Pow(10, (int)Math.Log10(Math.Abs(X))-(X<1 && X > -1?1:0)))

Test:

var inputs = new[] { 1700,170,17,1.7,0.17,0.017,0.0017,
                    -1700, -170, -17, -1.7, -0.17, -0.017, -0.0017 };

decimal  GetFractional(decimal X) =>
    (decimal)X/(decimal)(Math.Pow(10, (int)Math.Log10(Math.Abs(X))-(X<1 && X > -1?1:0)));

foreach (var x in inputs) 
{ 
    WriteLine($"{x,7}\t\t{GetFractional(x)}"); 
}

Result :

1700        1,7
 170        1,7
  17        1,7
 1,7        1,7
0,17        1,7
0,017       1,7
0,0017      1,7
-1700       -1,7
-170        -1,7
 -17        -1,7
-1,7        -1,7
-0,17       -1,7
-0,017      -1,7
-0,0017     -1,7
xdtTransform
  • 1,986
  • 14
  • 34
Mike Makarov
  • 1,287
  • 8
  • 17
1

Here would be an algorithmic approach to solve this:

public double ExtractFraction(double value)
{
    if (value == 0) return value;

    if (value > 10 || value < -10)
    {
        while (value > 10 || value < -10)
        {
            value /= 10.0;
        }
    }
    else if (value < 1 || value > -1)
    {
        while (value < 1 && value > -1)
        {
            value *= 10.0;
        }
    }
    return value;
}

Explanation: If the value is outside the range ]10 -10[ you need to divide by 10 until you slide inside this range

if the value is inside the range ]1 -1[ you need to multiply by 10 until you slide out of this range

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
0

This is how I extract the exponent and coefficient of a large number (valueToConvert) for a project I worked on:

int Exp = (int)Math.Log10(valueToConvert);
double Coef = (valueToConvert / Math.Pow(10,Exp)).ToString("f2");

For example, if valueToConvert = 5.43E+123, I get Coef = 5.43, Exp = 123.