Re: abs question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Prince

    #1

    Re: abs question

    Bill Cunningham wrote:
    ..... My
    system tells me abs is in stdlib.h and not just math.h. I don't know if
    that's true or not. Anyway abs takes ints and my variables are all doubles.
    abs() is of type int; fabs() was of type double in C89 <math.h>, it may be
    in <tgmath.hif your compiler has some C99 capability. Most compilers
    can generate single instruction in-line implementation of fabs().
    It's not difficult to check your "system," the answer would be in the
    compiler or library docs, and probably in the header files in the
    compiler's include folder.
    I don't see a connection with the source code you showed.
  • Bill Cunningham

    #2
    fabs question [was:abs question]


    "Tim Prince" <tprince@nospam computer.orgwro te in message
    news:VtzFk.275$ 8_3.131@flpi147 .ffdc.sbc.com.. .
    I don't see a connection with the source code you showed.
    The thing is I really don't know where to use fabs in this code. I've
    tried it in the printfs, for example,

    if (chr clr && pcr)
    printf("%.2f\n" , fabs(chr));
    if (clr chr && pcr)
    printf("%.2f\n" , fabs(clr));
    if (pcr chr && clr)
    printf("%.2f\n" , fabs(pcr));

    This doesn't help. :( This is a math question. Where to use fabs.

    Bill



    Comment

    • Tim Prince

      #3
      Re: fabs question [was:abs question]

      Bill Cunningham wrote:
      "Tim Prince" <tprince@nospam computer.orgwro te in message
      news:VtzFk.275$ 8_3.131@flpi147 .ffdc.sbc.com.. .
      >
      >I don't see a connection with the source code you showed.
      >
      The thing is I really don't know where to use fabs in this code. I've
      tried it in the printfs, for example,
      >
      if (chr clr && pcr)
      printf("%.2f\n" , fabs(chr));
      if (clr chr && pcr)
      printf("%.2f\n" , fabs(clr));
      if (pcr chr && clr)
      printf("%.2f\n" , fabs(pcr));
      >
      This doesn't help. :( This is a math question. Where to use fabs.
      >
      Nothing wrong with using it there, provided that you #include <math.hor
      <tgmath.h>. Turn on the warnings of your compiler to catch missing
      headers. If you don't understand the warnings, quote them when asking
      questions.

      Comment

      • Nick Keighley

        #4
        Re: fabs question [was:abs question]

        On 4 Oct, 03:09, "Bill Cunningham" <nos...@nspam.i nvalidwrote:
        "Tim Prince" <tpri...@nospam computer.orgwro te in message
        >
        news:VtzFk.275$ 8_3.131@flpi147 .ffdc.sbc.com.. .
        >
        I don't see a connection with the source code you showed.
        >
            The thing is I really don't know where to use fabs in this code. I've
        tried it in the printfs, for example,
        >
        if (chr clr && pcr)
                printf("%.2f\n" , fabs(chr));
            if (clr chr && pcr)
                printf("%.2f\n" , fabs(clr));
            if (pcr chr && clr)
                printf("%.2f\n" , fabs(pcr));
        >
        This doesn't help. :( This is a math question. Where to use fabs.
        You use fabs() when you want the absolute value of a floating
        point number (I can't be bothered to check if fabs() takes
        a float or a double).

        fabs(1.0) -1.0
        fabs(-1.0) -1.0

        I know nothing about finance so I have no idea if you actually
        want to calculate fabs or where.

        So
        - what output did you get?
        - what did you expect?

        You may have noticed me asking this before...

        --
        Nick Keighley




        Comment

        • Richard Heathfield

          #5
          Re: fabs question [was:abs question]

          Nick Keighley said:

          <snip>
          You use fabs() when you want the absolute value of a floating
          point number (I can't be bothered to check if fabs() takes
          a float or a double).
          It's a double.

          And *now* I'll look it up.

          4.5.6.2 The fabs function

          Synopsis

          #include <math.h>
          double fabs(double x);

          Description

          The fabs function computes the absolute value of a floating-point
          number x .

          Returns

          The fabs function returns the absolute value of x.

          I love being right. :-)

          --
          Richard Heathfield <http://www.cpax.org.uk >
          Email: -http://www. +rjh@
          Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
          "Usenet is a strange place" - dmr 29 July 1999

          Comment

          • Richard Tobin

            #6
            Re: fabs question [was:abs question]

            In article <b379584e-3bde-4d99-9c29-b39cc8594c7f@75 g2000hso.google groups.com>,
            Nick Keighley <nick_keighley_ nospam@hotmail. comwrote:
            >You use fabs() when you want the absolute value of a floating
            >point number (I can't be bothered to check if fabs() takes
            >a float or a double).
            fabs() predates prototypes, so it naturally takes a double because
            of the default promotions.

            -- Richard
            --
            Please remember to mention me / in tapes you leave behind.

            Comment

            Working...