Modulus with double variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Pippinger

    #1

    Modulus with double variables

    Hello NG,

    i need to calculate the modulus with double precisson typed variables.

    this works well with integers:
    w1 = w1 % 360;

    but what do i have to do if i want to use w1 as double?

    Thanks a lot!
    Peter

  • Jim Langston

    #2
    Re: Modulus with double variables

    "Peter Pippinger" <peter.pippinge r@gmx.de> wrote in message
    news:1148899545 .337492.249240@ 38g2000cwa.goog legroups.com...[color=blue]
    > Hello NG,
    >
    > i need to calculate the modulus with double precisson typed variables.
    >
    > this works well with integers:
    > w1 = w1 % 360;
    >
    > but what do i have to do if i want to use w1 as double?[/color]

    Well, you have the problem that floating point division does not have a
    remainder, otherwise it wouldn't be floating point. But, there is still a
    solution.

    Take for an example
    14.5 / 3.5;
    The answer is:
    4.1428571428571 428571428571428 571
    So what would the remainder be defined as? This would be how many times 3.5
    goes into 14.5 wholy. We see it goes 4 times plus a little bit. This is
    easy to figure out. Take the answer as an integer. Multiply it by the
    original divider. Then subtract this by the original divider.

    float a = 14.5;
    float b = 3.5;
    int result = static_cast<int >( a / b );
    float mod = a - static_cast<flo at>( result ) * b;




    Comment

    • Peter Pippinger

      #3
      Re: Modulus with double variables

      wow! That´s great. I have packed this in a function:

      //
      ---------------------------------------------------------------------
      // -- Modulus mit double Variablen
      //
      ---------------------------------------------------------------------
      double modulus(double a, double b)
      {
      int result = static_cast<int >( a / b );
      return a - static_cast<dou ble>( result ) * b;
      }

      Thanks a lot! you are great!

      Comment

      • Eric Jensen

        #4
        Re: Modulus with double variables


        "Peter Pippinger" <peter.pippinge r@gmx.de> skrev i en meddelelse
        news:1148899545 .337492.249240@ 38g2000cwa.goog legroups.com...[color=blue]
        > Hello NG,
        >
        > i need to calculate the modulus with double precisson typed variables.
        >
        > this works well with integers:
        > w1 = w1 % 360;
        >
        > but what do i have to do if i want to use w1 as double?
        >
        > Thanks a lot!
        > Peter
        >[/color]

        double fmod ( double x, double y ); // #include <cmath>

        double r,x=5.3,y=2;
        r = fmod(x,y); // r is not equal to 1.3

        //eric


        Comment

        • Alf P. Steinbach

          #5
          Re: Modulus with double variables

          * Peter Pippinger:[color=blue]
          >
          > i need to calculate the modulus with double precisson typed variables.
          >
          > this works well with integers:
          > w1 = w1 % 360;
          >
          > but what do i have to do if i want to use w1 as double?[/color]

          Check out round and floor.

          --
          A: Because it messes up the order in which people normally read text.
          Q: Why is it such a bad thing?
          A: Top-posting.
          Q: What is the most annoying thing on usenet and in e-mail?

          Comment

          • Steve Pope

            #6
            Re: Modulus with double variables

            Jim Langston <tazmaster@rock etmail.com> wrote:
            [color=blue]
            >float a = 14.5;
            >float b = 3.5;
            >int result = static_cast<int >( a / b );
            >float mod = a - static_cast<flo at>( result ) * b;[/color]

            I would worry that the static_cast would round to the
            nearest integer rather than truncating towards negative
            infinity.

            Steve

            Comment

            Working...