Exponents Major ?Bug?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shawn B.

    Exponents Major ?Bug?

    Greetings,

    I have the expression that works in Excel, Javascript, and VB.NET:

    B1 = 18

    10^(3*(B1)/1130)*6

    Result: 6.697903112


    When I evaluate in C#,


    float B1 = 18;
    double x;

    x = (3*B1/1130) * 6;
    x = Math.Pow(x, 10);

    I get the wrong result.

    Funny, when I add that Math.Pow part to VB.NET and JScript.NET, I get the
    same incorrect result.



    How can I get the correct result in C#?


    Thanks,
    Shawn


  • Alvin Bruney

    #2
    Re: Exponents Major ?Bug?

    Do we have to mindread to get the WRONG result value in C#?

    why mix float and doubles?

    --


    -----------
    Got TidBits?
    Get it here: www.networkip.net/tidbits
    "Shawn B." <leabre@html.co m> wrote in message
    news:uGvLvlanDH A.688@TK2MSFTNG P10.phx.gbl...[color=blue]
    > Greetings,
    >
    > I have the expression that works in Excel, Javascript, and VB.NET:
    >
    > B1 = 18
    >
    > 10^(3*(B1)/1130)*6
    >
    > Result: 6.697903112
    >
    >
    > When I evaluate in C#,
    >
    >
    > float B1 = 18;
    > double x;
    >
    > x = (3*B1/1130) * 6;
    > x = Math.Pow(x, 10);
    >
    > I get the wrong result.
    >
    > Funny, when I add that Math.Pow part to VB.NET and JScript.NET, I get the
    > same incorrect result.
    >
    >
    >
    > How can I get the correct result in C#?
    >
    >
    > Thanks,
    > Shawn
    >
    >[/color]


    Comment

    • Shawn B.

      #3
      Re: Exponents Major ?Bug?

      I would presume, the WRONG result to be anything other than the CORRECT
      result. If B1 was instead 18, and I place it in the stead of B1 in the C#
      expressions, it will still fail to result in the correct answer
      "6.69790311 2".


      Thanks,
      Shawn


      "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
      message news:%23Xb8apan DHA.964@TK2MSFT NGP10.phx.gbl.. .[color=blue]
      > Do we have to mindread to get the WRONG result value in C#?
      >
      > why mix float and doubles?
      >
      > --
      >
      >
      > -----------
      > Got TidBits?
      > Get it here: www.networkip.net/tidbits
      > "Shawn B." <leabre@html.co m> wrote in message
      > news:uGvLvlanDH A.688@TK2MSFTNG P10.phx.gbl...[color=green]
      > > Greetings,
      > >
      > > I have the expression that works in Excel, Javascript, and VB.NET:
      > >
      > > B1 = 18
      > >
      > > 10^(3*(B1)/1130)*6
      > >
      > > Result: 6.697903112
      > >
      > >
      > > When I evaluate in C#,
      > >
      > >
      > > float B1 = 18;
      > > double x;
      > >
      > > x = (3*B1/1130) * 6;
      > > x = Math.Pow(x, 10);
      > >
      > > I get the wrong result.
      > >
      > > Funny, when I add that Math.Pow part to VB.NET and JScript.NET, I get[/color][/color]
      the[color=blue][color=green]
      > > same incorrect result.
      > >
      > >
      > >
      > > How can I get the correct result in C#?
      > >
      > >
      > > Thanks,
      > > Shawn
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Shawn B.

        #4
        Re: Exponents Major ?Bug?

        If I cast B1,

        float B1 = 18;
        double x;
        double y=0;

        x = (3*(double)B1/1130) * 6;
        x = Math.Pow(x, 10);

        the incorrect answer would be: 3.7554818485097 7E-06
        which is the same as if I don't cast it. Mind you, if I user Math.Pow(x,
        10) in JScript.NET or VB.NET, I get the same incorrect answer. But if I use
        the power operators (available in those languages) instead of Math.Pow() I
        get the correct answer: 6.697903112


        Thanks,
        Shawn

        "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
        message news:%23Xb8apan DHA.964@TK2MSFT NGP10.phx.gbl.. .[color=blue]
        > Do we have to mindread to get the WRONG result value in C#?
        >
        > why mix float and doubles?
        >
        > --
        >
        >
        > -----------
        > Got TidBits?
        > Get it here: www.networkip.net/tidbits
        > "Shawn B." <leabre@html.co m> wrote in message
        > news:uGvLvlanDH A.688@TK2MSFTNG P10.phx.gbl...[color=green]
        > > Greetings,
        > >
        > > I have the expression that works in Excel, Javascript, and VB.NET:
        > >
        > > B1 = 18
        > >
        > > 10^(3*(B1)/1130)*6
        > >
        > > Result: 6.697903112
        > >
        > >
        > > When I evaluate in C#,
        > >
        > >
        > > float B1 = 18;
        > > double x;
        > >
        > > x = (3*B1/1130) * 6;
        > > x = Math.Pow(x, 10);
        > >
        > > I get the wrong result.
        > >
        > > Funny, when I add that Math.Pow part to VB.NET and JScript.NET, I get[/color][/color]
        the[color=blue][color=green]
        > > same incorrect result.
        > >
        > >
        > >
        > > How can I get the correct result in C#?
        > >
        > >
        > > Thanks,
        > > Shawn
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • mikeb

          #5
          Re: Exponents Major ?Bug?

          Shawn B. wrote:[color=blue]
          > Greetings,
          >
          > I have the expression that works in Excel, Javascript, and VB.NET:
          >
          > B1 = 18
          >
          > 10^(3*(B1)/1130)*6
          >
          > Result: 6.697903112
          >
          >
          > When I evaluate in C#,
          >
          >
          > float B1 = 18;
          > double x;
          >
          > x = (3*B1/1130) * 6;
          > x = Math.Pow(x, 10);
          >
          > I get the wrong result.[/color]

          You get a different result because the expressions are not equivalent.

          The C# equivalent of your Excel, JavaScript & VB.NET expression is:

          Math.Pow( 10, (3*B1/1130)) * 6

          The exponent is (3*B1/1130), which should be the 2nd parameter to
          Math.Pow().

          Note that 6 is not passed to Math.Pow() - it's not part of the exponent
          in your original expression.
          [color=blue]
          >
          > Funny, when I add that Math.Pow part to VB.NET and JScript.NET, I get the
          > same incorrect result.
          >
          >
          >
          > How can I get the correct result in C#?
          >
          >
          > Thanks,
          > Shawn
          >
          >[/color]

          Comment

          • Jay B. Harlow [MVP - Outlook]

            #6
            Re: Exponents Major ?Bug?

            Shawn,
            C# gives you a different answer, as you gave C# a different problem! :-)

            Remember that Math.Pow(x, y) returns x ^ y.
            [color=blue]
            > float B1 = 18;
            > double x;
            >
            > x = (3*B1/1130) * 6;
            > x = Math.Pow(x, 10);[/color]

            Asks C# to solve:

            ((3*(B1)/1130)*6) ^ 10

            If you flip the parameters to Math.Pow, you get the answer you expect!
            [color=blue]
            > x = Math.Pow(10, x);[/color]

            Hope this helps
            Jay


            "Shawn B." <leabre@html.co m> wrote in message
            news:uGvLvlanDH A.688@TK2MSFTNG P10.phx.gbl...[color=blue]
            > Greetings,
            >
            > I have the expression that works in Excel, Javascript, and VB.NET:
            >
            > B1 = 18
            >
            > 10^(3*(B1)/1130)*6
            >
            > Result: 6.697903112
            >
            >
            > When I evaluate in C#,
            >
            >
            > float B1 = 18;
            > double x;
            >
            > x = (3*B1/1130) * 6;
            > x = Math.Pow(x, 10);
            >
            > I get the wrong result.
            >
            > Funny, when I add that Math.Pow part to VB.NET and JScript.NET, I get the
            > same incorrect result.
            >
            >
            >
            > How can I get the correct result in C#?
            >
            >
            > Thanks,
            > Shawn
            >
            >[/color]


            Comment

            • Jay B. Harlow [MVP - Outlook]

              #7
              Re: Exponents Major ?Bug?

              Mike,
              Doh! I saw the flipped parameters, I missed the parenthesis.

              Shawn, Mike's answer is more correct ;-)

              Jay

              "mikeb" <mailbox.google @mailnull.com> wrote in message
              news:%23rnml4an DHA.1948@TK2MSF TNGP12.phx.gbl. ..[color=blue]
              > Shawn B. wrote:[color=green]
              > > Greetings,
              > >
              > > I have the expression that works in Excel, Javascript, and VB.NET:
              > >
              > > B1 = 18
              > >
              > > 10^(3*(B1)/1130)*6
              > >
              > > Result: 6.697903112
              > >
              > >
              > > When I evaluate in C#,
              > >
              > >
              > > float B1 = 18;
              > > double x;
              > >
              > > x = (3*B1/1130) * 6;
              > > x = Math.Pow(x, 10);
              > >
              > > I get the wrong result.[/color]
              >
              > You get a different result because the expressions are not equivalent.
              >
              > The C# equivalent of your Excel, JavaScript & VB.NET expression is:
              >
              > Math.Pow( 10, (3*B1/1130)) * 6
              >
              > The exponent is (3*B1/1130), which should be the 2nd parameter to
              > Math.Pow().
              >
              > Note that 6 is not passed to Math.Pow() - it's not part of the exponent
              > in your original expression.
              >[color=green]
              > >
              > > Funny, when I add that Math.Pow part to VB.NET and JScript.NET, I get[/color][/color]
              the[color=blue][color=green]
              > > same incorrect result.
              > >
              > >
              > >
              > > How can I get the correct result in C#?
              > >
              > >
              > > Thanks,
              > > Shawn
              > >
              > >[/color]
              >[/color]


              Comment

              • Shawn B.

                #8
                Re: Exponents Major ?Bug?

                Thank you.


                Thanks,
                Shawn



                "mikeb" <mailbox.google @mailnull.com> wrote in message
                news:%23rnml4an DHA.1948@TK2MSF TNGP12.phx.gbl. ..[color=blue]
                > Shawn B. wrote:[color=green]
                > > Greetings,
                > >
                > > I have the expression that works in Excel, Javascript, and VB.NET:
                > >
                > > B1 = 18
                > >
                > > 10^(3*(B1)/1130)*6
                > >
                > > Result: 6.697903112
                > >
                > >
                > > When I evaluate in C#,
                > >
                > >
                > > float B1 = 18;
                > > double x;
                > >
                > > x = (3*B1/1130) * 6;
                > > x = Math.Pow(x, 10);
                > >
                > > I get the wrong result.[/color]
                >
                > You get a different result because the expressions are not equivalent.
                >
                > The C# equivalent of your Excel, JavaScript & VB.NET expression is:
                >
                > Math.Pow( 10, (3*B1/1130)) * 6
                >
                > The exponent is (3*B1/1130), which should be the 2nd parameter to
                > Math.Pow().
                >
                > Note that 6 is not passed to Math.Pow() - it's not part of the exponent
                > in your original expression.
                >[color=green]
                > >
                > > Funny, when I add that Math.Pow part to VB.NET and JScript.NET, I get[/color][/color]
                the[color=blue][color=green]
                > > same incorrect result.
                > >
                > >
                > >
                > > How can I get the correct result in C#?
                > >
                > >
                > > Thanks,
                > > Shawn
                > >
                > >[/color]
                >[/color]


                Comment

                • Alvin Bruney

                  #9
                  Re: Exponents Major ?Bug?

                  Jack:
                  Do you get a kick out of spoofing my name and email to be an idiot? Does
                  that make you feel better to pass yourself off as me? You need to grow up,
                  and stop using my name and email as yours please.

                  --


                  -----------
                  Got TidBits?
                  Get it here: www.networkip.net/tidbits
                  "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
                  message news:#Xb8apanDH A.964@TK2MSFTNG P10.phx.gbl...[color=blue]
                  > Do we have to mindread to get the WRONG result value in C#?
                  >
                  > why mix float and doubles?
                  >
                  > --
                  >
                  >
                  > -----------
                  > Got TidBits?
                  > Get it here: www.networkip.net/tidbits
                  > "Shawn B." <leabre@html.co m> wrote in message
                  > news:uGvLvlanDH A.688@TK2MSFTNG P10.phx.gbl...[color=green]
                  > > Greetings,
                  > >
                  > > I have the expression that works in Excel, Javascript, and VB.NET:
                  > >
                  > > B1 = 18
                  > >
                  > > 10^(3*(B1)/1130)*6
                  > >
                  > > Result: 6.697903112
                  > >
                  > >
                  > > When I evaluate in C#,
                  > >
                  > >
                  > > float B1 = 18;
                  > > double x;
                  > >
                  > > x = (3*B1/1130) * 6;
                  > > x = Math.Pow(x, 10);
                  > >
                  > > I get the wrong result.
                  > >
                  > > Funny, when I add that Math.Pow part to VB.NET and JScript.NET, I get[/color][/color]
                  the[color=blue][color=green]
                  > > same incorrect result.
                  > >
                  > >
                  > >
                  > > How can I get the correct result in C#?
                  > >
                  > >
                  > > Thanks,
                  > > Shawn
                  > >
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  Working...