Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to do Modulo (double and Big Numbers)

How to do Modulo (double and Big Numbers)

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestioncareer
10 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Schehaider_Aymen
    wrote on last edited by
    #1

    Hi all. I was wondering why a modf ( modulo for float doesn't do its job : I explain) i have a Big Number X = 1051450100212345678903111694, so as i can't handle it into regular int, _int64, long int i used the double var type. but the % (modulo) for float is located in cmath by the modf function so when i want to calculate

    double x = 1051450100212345678903111694;
    double BigX = 100 * x;
    double y = 97;

    double z = modf(BigX, & y);

    i always have 0 in z

    ok why not writing some code doing the regular job.

    while (BigX >97)
    BigX = BigX - (BigX/97);

    ok But in the second code the result will 96.0111 (sth like that) or when i do it with the Calculator of windows it shows me 92 which differs so how could i do such modulo for big numbers ??? Thx U

    "The Ultimate Limit Is Only Your Imagination."

    L CPalliniC P A 4 Replies Last reply
    0
    • S Schehaider_Aymen

      Hi all. I was wondering why a modf ( modulo for float doesn't do its job : I explain) i have a Big Number X = 1051450100212345678903111694, so as i can't handle it into regular int, _int64, long int i used the double var type. but the % (modulo) for float is located in cmath by the modf function so when i want to calculate

      double x = 1051450100212345678903111694;
      double BigX = 100 * x;
      double y = 97;

      double z = modf(BigX, & y);

      i always have 0 in z

      ok why not writing some code doing the regular job.

      while (BigX >97)
      BigX = BigX - (BigX/97);

      ok But in the second code the result will 96.0111 (sth like that) or when i do it with the Calculator of windows it shows me 92 which differs so how could i do such modulo for big numbers ??? Thx U

      "The Ultimate Limit Is Only Your Imagination."

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Blood_HaZaRd wrote:

      i always have 0 in z

      That's because there is no fractional part in your value; both x and BigX in your sample are whole numbers. See the description of modf()[^].

      It's time for a new signature.

      S 1 Reply Last reply
      0
      • S Schehaider_Aymen

        Hi all. I was wondering why a modf ( modulo for float doesn't do its job : I explain) i have a Big Number X = 1051450100212345678903111694, so as i can't handle it into regular int, _int64, long int i used the double var type. but the % (modulo) for float is located in cmath by the modf function so when i want to calculate

        double x = 1051450100212345678903111694;
        double BigX = 100 * x;
        double y = 97;

        double z = modf(BigX, & y);

        i always have 0 in z

        ok why not writing some code doing the regular job.

        while (BigX >97)
        BigX = BigX - (BigX/97);

        ok But in the second code the result will 96.0111 (sth like that) or when i do it with the Calculator of windows it shows me 92 which differs so how could i do such modulo for big numbers ??? Thx U

        "The Ultimate Limit Is Only Your Imagination."

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        Blood_HaZaRd wrote:

        i have a Big Number X = 1051450100212345678903111694, so as i can't handle it into regular int, _int64, long int i used the double var type.

        A double cannot handle it with the necessary accuracy, the following code

        double x = 1051450100212345678903111694.;
        printf("%20f",x);

        outputs

        1051450100212345700000000000.000000

        :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        • S Schehaider_Aymen

          Hi all. I was wondering why a modf ( modulo for float doesn't do its job : I explain) i have a Big Number X = 1051450100212345678903111694, so as i can't handle it into regular int, _int64, long int i used the double var type. but the % (modulo) for float is located in cmath by the modf function so when i want to calculate

          double x = 1051450100212345678903111694;
          double BigX = 100 * x;
          double y = 97;

          double z = modf(BigX, & y);

          i always have 0 in z

          ok why not writing some code doing the regular job.

          while (BigX >97)
          BigX = BigX - (BigX/97);

          ok But in the second code the result will 96.0111 (sth like that) or when i do it with the Calculator of windows it shows me 92 which differs so how could i do such modulo for big numbers ??? Thx U

          "The Ultimate Limit Is Only Your Imagination."

          P Offline
          P Offline
          Peter_in_2780
          wrote on last edited by
          #4

          You will need to get (or write) a big-integer library. Google will find lots, such as http://mattmccutchen.net/bigint/[^] Disclaimer: I have nothing to do with Matt, except that I downloaded his library.

          Software rusts. Simon Stephenson, ca 1994.

          S 2 Replies Last reply
          0
          • L Lost User

            Blood_HaZaRd wrote:

            i always have 0 in z

            That's because there is no fractional part in your value; both x and BigX in your sample are whole numbers. See the description of modf()[^].

            It's time for a new signature.

            S Offline
            S Offline
            Schehaider_Aymen
            wrote on last edited by
            #5

            Thx It seems That i missunderstood The modf . ^^ thx ;P

            "The Ultimate Limit Is Only Your Imagination."

            1 Reply Last reply
            0
            • P Peter_in_2780

              You will need to get (or write) a big-integer library. Google will find lots, such as http://mattmccutchen.net/bigint/[^] Disclaimer: I have nothing to do with Matt, except that I downloaded his library.

              Software rusts. Simon Stephenson, ca 1994.

              S Offline
              S Offline
              Schehaider_Aymen
              wrote on last edited by
              #6

              Great Library but could i Use some Commons var, i don't like going for others library :p

              "The Ultimate Limit Is Only Your Imagination."

              1 Reply Last reply
              0
              • P Peter_in_2780

                You will need to get (or write) a big-integer library. Google will find lots, such as http://mattmccutchen.net/bigint/[^] Disclaimer: I have nothing to do with Matt, except that I downloaded his library.

                Software rusts. Simon Stephenson, ca 1994.

                S Offline
                S Offline
                Schehaider_Aymen
                wrote on last edited by
                #7

                i 've got errors

                error C2258: illegal pure syntax, must be '= 0'
                error C2252: 'less' : pure specifier can only be specified for functions
                error C2258: illegal pure syntax, must be '= 0'
                error C2252: 'equal' : pure specifier can only be specified for functions
                error C2258: illegal pure syntax, must be '= 0'
                error C2252: 'greater' : pure specifier can only be specified for functions

                Just here

                class BigInteger {

                public:
                typedef BigUnsigned::Blk Blk;
                typedef BigUnsigned::Index Index;
                typedef BigUnsigned::CmpRes CmpRes;
                static const CmpRes
                less = BigUnsigned::less ,
                equal = BigUnsigned::equal ,
                greater = BigUnsigned::greater;
                // Enumeration for the sign of a BigInteger.
                enum Sign { negative = -1, zero = 0, positive = 1 };

                protected:
                Sign sign;
                BigUnsigned mag;

                :omg:

                "The Ultimate Limit Is Only Your Imagination."

                N 1 Reply Last reply
                0
                • S Schehaider_Aymen

                  i 've got errors

                  error C2258: illegal pure syntax, must be '= 0'
                  error C2252: 'less' : pure specifier can only be specified for functions
                  error C2258: illegal pure syntax, must be '= 0'
                  error C2252: 'equal' : pure specifier can only be specified for functions
                  error C2258: illegal pure syntax, must be '= 0'
                  error C2252: 'greater' : pure specifier can only be specified for functions

                  Just here

                  class BigInteger {

                  public:
                  typedef BigUnsigned::Blk Blk;
                  typedef BigUnsigned::Index Index;
                  typedef BigUnsigned::CmpRes CmpRes;
                  static const CmpRes
                  less = BigUnsigned::less ,
                  equal = BigUnsigned::equal ,
                  greater = BigUnsigned::greater;
                  // Enumeration for the sign of a BigInteger.
                  enum Sign { negative = -1, zero = 0, positive = 1 };

                  protected:
                  Sign sign;
                  BigUnsigned mag;

                  :omg:

                  "The Ultimate Limit Is Only Your Imagination."

                  N Offline
                  N Offline
                  Niklas L
                  wrote on last edited by
                  #8

                  Looks like you use an old compiler not supporting initialization of static const vars in class declaration. Initialize them in cpp-file instead if BigInteger is your own class.

                  home

                  S 1 Reply Last reply
                  0
                  • N Niklas L

                    Looks like you use an old compiler not supporting initialization of static const vars in class declaration. Initialize them in cpp-file instead if BigInteger is your own class.

                    home

                    S Offline
                    S Offline
                    Schehaider_Aymen
                    wrote on last edited by
                    #9

                    lool ok how Unlucky i am

                    "The Ultimate Limit Is Only Your Imagination."

                    1 Reply Last reply
                    0
                    • S Schehaider_Aymen

                      Hi all. I was wondering why a modf ( modulo for float doesn't do its job : I explain) i have a Big Number X = 1051450100212345678903111694, so as i can't handle it into regular int, _int64, long int i used the double var type. but the % (modulo) for float is located in cmath by the modf function so when i want to calculate

                      double x = 1051450100212345678903111694;
                      double BigX = 100 * x;
                      double y = 97;

                      double z = modf(BigX, & y);

                      i always have 0 in z

                      ok why not writing some code doing the regular job.

                      while (BigX >97)
                      BigX = BigX - (BigX/97);

                      ok But in the second code the result will 96.0111 (sth like that) or when i do it with the Calculator of windows it shows me 92 which differs so how could i do such modulo for big numbers ??? Thx U

                      "The Ultimate Limit Is Only Your Imagination."

                      A Offline
                      A Offline
                      Aescleal
                      wrote on last edited by
                      #10

                      I'm a bit confused: 0xE97804B9A34AB4E fits in an __int64 with a nibble to spare. I know it depends what your compiler is and __int64 isn't a standard type but if you've got it and it doesn't need to be portable then why not use it? Ash PS: I'm normally against using non-portable code but at least someone trying to compile your code for a platform with a 64 bit int would at least be warned something was up. PPS: And if you need to store it in 32 bit values you can take advantage of some properties of ints that makes it relatively easy to calculate the modulus.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups