Thread: Under/Overflow you do handle them

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    39

    Under/Overflow you do handle them

    I have a function that gives me log(y) and derivative of log(y) call it dlog(y) . However I am interested in knowing y and derivative of y call it dy. Now I get dy as follows
    Code:
    dy = exp( log (y)) * dlog ( y)
    which is equaivalent to
    
    dy = exp( log (y)) * (1/ y * dy)
    
    which simplifies to
    
    dy = dy
    I get under/overflow problems when y is close to zero. Any suggestions?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Floating point numbers are notoriously inaccurate. You cannot expect to get every number from .0000000001 (or whatever small number) up to 1 successfully stored in 32 bits. (Or whatever bit representation you want.) At best you can approximate it, and that's what floating point numbers do. There are huge write ups on the topic, but I've never found them interesting enough to read. If you really want to know: Single precision floating-point format - Wikipedia, the free encyclopedia

    At least I'm going to assume that is what your question is about.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I'd be interested in knowing how you know you're getting overflow and underflow... since C includes no runtime error trapping.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    39
    Because in another part of the code when I switched from exp to expl it got rid of some problems. I am guessing it is an over/underflow problem because for a sequence of numbers that get closer to zero, I start getting rubbish answers.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,654
    You should really read this.
    What Every Computer Scientist Should Know About Floating-Point Arithmetic
    In particular, everything where relative error is discussed.

    If you're doing serious floating point work, you need to be able to analyse where these kinds of errors are introduced, and how to write the code in such a way as to minimise the effects. As the paper discusses, x^2 - y^2 is mathematically identical to (x - y)(x + y), but on a machine with a finite number of bits, there are some surprises waiting. In short, you can't simply copy/paste your maths directly into 'C' code and hope that it will all work out.

    Because statements like "when I switched from exp to expl it got rid of some problems" mean you're just stumbling around in the dark walking into things.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ali.franco95 View Post
    Because in another part of the code when I switched from exp to expl it got rid of some problems. I am guessing it is an over/underflow problem because for a sequence of numbers that get closer to zero, I start getting rubbish answers.
    Most likely you are running into the well known inaccuracies of floating point math...

    In post #5 Salem provides a link to an excellent discussion of the problem... It's the red text, click and read...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Which is a better practice? (Global handle or finding handle)
    By C_Sparky in forum Windows Programming
    Replies: 4
    Last Post: 12-31-2010, 07:32 PM
  2. int overflow
    By nimitzhunter in forum C++ Programming
    Replies: 9
    Last Post: 12-12-2010, 01:36 AM
  3. overflow...?
    By Tool in forum C Programming
    Replies: 10
    Last Post: 12-23-2009, 03:41 PM
  4. Overflow
    By valthyx in forum C Programming
    Replies: 14
    Last Post: 06-01-2009, 04:41 PM
  5. how to handle integer overflow in C
    By kate1234 in forum C Programming
    Replies: 8
    Last Post: 04-23-2003, 12:20 PM