math q

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bill Cunningham

    math q

    Here's my code as of latest:

    #include <stdio.h>
    #include <math.h>

    int main() {
    if ((printf("%d",p ow(2,128))==0)
    { fprintf (stderr,"0 error");
    return -1;
    }
    return 0;
    }

    I realize there is also a C99 exp2 ( ) function that would do the same
    thing. I originally had this.

    #include <stdio.h>
    #include <math.h>

    int main() {
    printf("%d",pow (2,128));
    return 0;
    }

    I have tried to add an error routine to the first code. This is going to be
    a big number but i'm looking to write 128 bit strings and or numbers that
    are unique like MS's clsids and GUIDs. First I need to get a decimal value.
    Oh yes is there any way I can cast a hex number in printf to get a hex
    instead of pow ( ) 's double type.

    Bill
    I hope this is clear.


  • santosh

    #2
    Re: math q

    Bill Cunningham wrote:
    Here's my code as of latest:
    >
    #include <stdio.h>
    #include <math.h>
    >
    int main() {
    if ((printf("%d",p ow(2,128))==0)
    { fprintf (stderr,"0 error");
    return -1;
    }
    return 0;
    }
    That's not compilable. And after fixing it's still utterly wrong.
    I realize there is also a C99 exp2 ( ) function that would do the same
    thing. I originally had this.
    >
    #include <stdio.h>
    #include <math.h>
    >
    int main() {
    printf("%d",pow (2,128));
    Pow returns a double value. The format specifier for a double is %f. You
    might also want to use long double and powl with the %Lf specifier.

    These functions can also overflow or underflow.
    return 0;
    }
    >
    I have tried to add an error routine to the first code. This is going
    to be a big number but i'm looking to write 128 bit strings and or
    numbers that are unique like MS's clsids and GUIDs. First I need to
    get a decimal value. Oh yes is there any way I can cast a hex number
    in printf to get a hex instead of pow ( ) 's double type.
    >
    Bill
    I hope this is clear.
    No it isn't, but if you want very large integer values, then you'll
    probably have to use an extended precision arithmetic package like GNU
    GMP.

    PS. There are precanned routines for generating UUIDs. Why not use one?

    Comment

    • Morris Dovey

      #3
      Re: math q

      Bill Cunningham wrote:
      >
      Here's my code as of latest:
      >
      #include <stdio.h>
      #include <math.h>
      >
      int main() {
      if ((printf("%d",p ow(2,128))==0)
      { fprintf (stderr,"0 error");
      return -1;
      }
      return 0;
      }
      You might want to notice that pow() returns a double.

      --
      Morris Dovey
      DeSoto Solar
      DeSoto, Iowa USA

      Comment

      • Bill Cunningham

        #4
        Re: math q


        "Morris Dovey" <mrdovey@iedu.c omwrote in message
        news:47DAE9B5.1 E4F63B5@iedu.co m...
        Bill Cunningham wrote:
        >>
        > Here's my code as of latest:
        >>
        >#include <stdio.h>
        >#include <math.h>
        >>
        >int main() {
        > if ((printf("%d",p ow(2,128))==0)
        > { fprintf (stderr,"0 error");
        > return -1;
        > }
        > return 0;
        > }
        >
        You might want to notice that pow() returns a double.
        OK an oversight. I thought %d was a double and %f a floating point
        number.

        Bill


        Comment

        • Bill Cunningham

          #5
          Re: math q


          "santosh" <santosh.k83@gm ail.comwrote in message
          news:fresuf$pvc $1@registered.m otzarella.org.. .
          Bill Cunningham wrote:
          >
          > Here's my code as of latest:
          >>
          >#include <stdio.h>
          >#include <math.h>
          >>
          >int main() {
          > if ((printf("%d",p ow(2,128))==0)
          > { fprintf (stderr,"0 error");
          > return -1;
          > }
          > return 0;
          > }
          >
          That's not compilable. And after fixing it's still utterly wrong.
          >
          >I realize there is also a C99 exp2 ( ) function that would do the same
          >thing. I originally had this.
          >>
          >#include <stdio.h>
          >#include <math.h>
          >>
          >int main() {
          > printf("%d",pow (2,128));
          >
          Pow returns a double value. The format specifier for a double is %f. You
          might also want to use long double and powl with the %Lf specifier.
          OK I thought %d was a double and %f a floating point.
          These functions can also overflow or underflow.
          >
          > return 0;
          >}
          >>
          >I have tried to add an error routine to the first code. This is going
          >to be a big number but i'm looking to write 128 bit strings and or
          >numbers that are unique like MS's clsids and GUIDs. First I need to
          >get a decimal value. Oh yes is there any way I can cast a hex number
          >in printf to get a hex instead of pow ( ) 's double type.
          >>
          >Bill
          >I hope this is clear.
          >
          No it isn't, but if you want very large integer values, then you'll
          probably have to use an extended precision arithmetic package like GNU
          GMP.
          >
          PS. There are precanned routines for generating UUIDs. Why not use one?
          I will look for one. GMP is that also called gimp?

          Bill


          Comment

          • santosh

            #6
            Re: math q

            Bill Cunningham wrote:
            >
            "santosh" <santosh.k83@gm ail.comwrote in message
            news:fresuf$pvc $1@registered.m otzarella.org.. .
            >Bill Cunningham wrote:
            >>
            >> Here's my code as of latest:
            >>>
            >>#include <stdio.h>
            >>#include <math.h>
            >>>
            >>int main() {
            >> if ((printf("%d",p ow(2,128))==0)
            >> { fprintf (stderr,"0 error");
            >> return -1;
            >> }
            >> return 0;
            >> }
            >>
            >That's not compilable. And after fixing it's still utterly wrong.
            >>
            >>I realize there is also a C99 exp2 ( ) function that would do the
            >>same thing. I originally had this.
            >>>
            >>#include <stdio.h>
            >>#include <math.h>
            >>>
            >>int main() {
            >> printf("%d",pow (2,128));
            >>
            >Pow returns a double value. The format specifier for a double is %f.
            >You might also want to use long double and powl with the %Lf
            >specifier.
            >
            OK I thought %d was a double and %f a floating point.
            No. %d is for printing an int in decimal representation. %f is for
            printing float and double arguments, as is %e, %E, %g, and %G. You can
            use the 'L' modifier with the above specifiers to print a long double
            argument.
            >These functions can also overflow or underflow.
            >>
            >> return 0;
            >>}
            >>>
            >>I have tried to add an error routine to the first code. This is
            >>going to be a big number but i'm looking to write 128 bit strings
            >>and or numbers that are unique like MS's clsids and GUIDs. First I
            >>need to get a decimal value. Oh yes is there any way I can cast a
            >>hex number in printf to get a hex instead of pow ( ) 's double type.
            >>>
            >>Bill
            >>I hope this is clear.
            >>
            >No it isn't, but if you want very large integer values, then you'll
            >probably have to use an extended precision arithmetic package like
            >GNU GMP.
            >>
            >PS. There are precanned routines for generating UUIDs. Why not use
            >one?
            >
            I will look for one. GMP is that also called gimp?
            No. Go here:

            <http://gmplib.org/>

            Describe what you are actually trying to do. Do you want to generate 128
            bit integer values? What for? What are you actually going to do with
            them?

            Comment

            • Bill Cunningham

              #7
              Re: math q

              Describe what you are actually trying to do. Do you want to generate 128
              bit integer values? What for? What are you actually going to do with
              them?
              Actually I'm using this as a learning experience. The more I read and
              fiddle with C I seem to pick up more. I wanted to see if I could write my
              own generator. As for doing much with them I don't really know. That's the
              joy of exploring in my opinion.

              When you said about my first code it wasn't compilable do you see what I
              was trying to do ? I could use errno.h and generate error codes. When I get
              0 I don't know if there's a overflow or I'm coding wrong. I was trying to
              add error checking to the second code I wrote.

              Bill


              Comment

              • Eric Sosman

                #8
                Re: math q

                Bill Cunningham wrote:
                [...] Actually I'm using this as a learning experience. [...]
                Your first, it appears.

                --
                Eric Sosman
                esosman@ieee-dot-org.invalid

                Comment

                • santosh

                  #9
                  Re: math q

                  Bill Cunningham wrote:
                  >
                  >Describe what you are actually trying to do. Do you want to generate
                  >128 bit integer values? What for? What are you actually going to do
                  >with them?
                  [...]
                  When you said about my first code it wasn't compilable do you see
                  what I was trying to do ?
                  No.
                  I could use errno.h and generate error codes.
                  Yes. That's the proper method.
                  When I get 0 I don't know if there's a overflow or I'm
                  coding wrong.
                  Both in your case. Use long double. If you want to represent the value
                  in an integer then you need a bignum package like GMP.
                  I was trying to add error checking to the second
                  code I wrote.
                  But it was wrong.

                  Comment

                  • Bill Cunningham

                    #10
                    Re: math q


                    "santosh" <santosh.k83@gm ail.comwrote in message
                    news:frg26q$7lo $1@registered.m otzarella.org.. .
                    Bill Cunningham wrote:
                    >
                    >>
                    >>Describe what you are actually trying to do. Do you want to generate
                    >>128 bit integer values? What for? What are you actually going to do
                    >>with them?
                    [...]
                    >
                    > When you said about my first code it wasn't compilable do you see
                    > what I was trying to do ?
                    >
                    No.
                    >
                    > I could use errno.h and generate error codes.
                    >
                    Yes. That's the proper method.
                    >
                    > When I get 0 I don't know if there's a overflow or I'm
                    > coding wrong.
                    >
                    Both in your case. Use long double. If you want to represent the value
                    in an integer then you need a bignum package like GMP.
                    >
                    > I was trying to add error checking to the second
                    > code I wrote.
                    >
                    But it was wrong.
                    Thanks Santosh. But out of curiosity before errno.h how would error
                    checking or a diagnostic method have been used in this situation? My long
                    doubles are 12 bytes on this system while double are 8. The number generated
                    once I cleared up the printf specifier problem had many zeros on the end so
                    a long double is no doubt needed.

                    ps how is my style now ? Much more readable?

                    Bill


                    Comment

                    • santosh

                      #11
                      Re: math q

                      Bill Cunningham wrote:
                      >
                      "santosh" <santosh.k83@gm ail.comwrote in message
                      news:frg26q$7lo $1@registered.m otzarella.org.. .
                      >Bill Cunningham wrote:
                      >>
                      >>>
                      >>>Describe what you are actually trying to do. Do you want to
                      >>>generate 128 bit integer values? What for? What are you actually
                      >>>going to do with them?
                      >[...]
                      >>
                      >> When you said about my first code it wasn't compilable do you
                      >> see what I was trying to do ?
                      >>
                      >No.
                      >>
                      >> I could use errno.h and generate error codes.
                      >>
                      >Yes. That's the proper method.
                      >>
                      >> When I get 0 I don't know if there's a overflow or I'm
                      >> coding wrong.
                      >>
                      >Both in your case. Use long double. If you want to represent the
                      >value in an integer then you need a bignum package like GMP.
                      >>
                      >> I was trying to add error checking to the second
                      >> code I wrote.
                      >>
                      >But it was wrong.
                      >
                      Thanks Santosh. But out of curiosity before errno.h how would
                      error
                      checking or a diagnostic method have been used in this situation?
                      Actually the Standard does not specify whether pow/powf/powl will set
                      errno if an overflow or underflow occurs. On my system for example
                      errno is not set for overflow of pow(2, 1288), but it *is* set for the
                      call powl(2, 12888899999999) . It's a quality of implementation issue.
                      All it says is that a "range error" *may* occur if the result is out of
                      range for the result type or a "domain error" will occur if the first
                      argument is finite and negative and the second is finite and not an
                      integer value.

                      So for pow/powf/powl there is no sure way to detect a range error. It
                      depends on what your compiler does. You can however, set errno to zero
                      before the call to pow/powf/powl and then test errno to see whether it
                      is set to ERANGE. You might also want to output the system's error
                      message through perror or sterror.

                      Actually C99 has added a lot of functions and macros that you can use to
                      test whether additional guarantees are made by your implementation. For
                      example you can test MATH_ERRNO to see whether a math function sets
                      errno to a non-zero value. You can also use it in conjunction with the
                      macro math_errhandlin g. There are many other details that you probably
                      don't want to know.

                      My advice is if there is a chance that your calculations may overflow or
                      underflow a long double, then use an extended precision math package
                      like GNU GMP. It reduces portability, but it might not matter in your
                      case.
                      My
                      long doubles are 12 bytes on this system while double are 8. The
                      number generated once I cleared up the printf specifier problem had
                      many zeros on the end so a long double is no doubt needed.
                      A double of eight bytes is most likely sufficient for calculating 2^128.
                      Check with DBL_MAX just to be sure.

                      <snip>

                      Comment

                      Working...