Why different from expectation

Discussion in 'C Programming' started by yezi, Nov 28, 2005.

  1. yezi

    yezi Guest

    Hi: All:

    I have question with the data type problem. According to the C
    standard. IEEE 754

    float 4 3.4x10-38E..3.4x10+38E
    double 8 1.7x10-308E..1.7x10+308E
    long double 12 ???

    The code function is (0.000200-0.000300)*(0.000200-0.000300)
    suppose the result (difference ) should be the 10 (-8)

    difference I claim is double;

    But THe code result is :
    fp1 0.000200
    ------------------------
    fp2 0.000300
    difference is 0.000000

    which means the result is zero.

    Why?

    Thanks
    bin YE
    yezi, Nov 28, 2005
    #1
    1. Advertising

  2. "yezi" <> writes:
    > I have question with the data type problem. According to the C
    > standard. IEEE 754
    >
    > float 4 3.4x10-38E..3.4x10+38E
    > double 8 1.7x10-308E..1.7x10+308E
    > long double 12 ???


    The C standard allows, but does not require, IEEE 754 floating-point.

    > The code function is (0.000200-0.000300)*(0.000200-0.000300)
    > suppose the result (difference ) should be the 10 (-8)
    >
    > difference I claim is double;
    >
    > But THe code result is :
    > fp1 0.000200
    > ------------------------
    > fp2 0.000300
    > difference is 0.000000
    >
    > which means the result is zero.


    It's difficult to tell what problem you're describing. Try posting
    some actual code along with the output it produces, and describe how
    it differs from the output you expected.

    A couple of thoughts:

    Floating-point is inexact. For example, the value 0.1 cannot be
    represented exactly in binary floating-point; the closest equivalent
    in type double, on one system I've tried, is about
    0.10000000000000000555111512312578.

    If you're using a "%f" output format, it's not going to show very
    small values very well. A printed result of 0.000000 doesn't imply
    that the value is actually equal to 0.0, just that it's within a
    certain range close to 0.0 (which 1.0e-8 probably is). Try the "%g"
    format instead; it switches to scientific notation for very large or
    very small values.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Nov 28, 2005
    #2
    1. Advertising

  3. "yezi" <> writes:
    > I have question with the data type problem. According to the C
    > standard. IEEE 754
    >
    > float 4 3.4x10-38E..3.4x10+38E
    > double 8 1.7x10-308E..1.7x10+308E
    > long double 12 ???
    >
    > The code function is (0.000200-0.000300)*(0.000200-0.000300)
    > suppose the result (difference ) should be the 10 (-8)
    >
    > difference I claim is double;
    >
    > But THe code result is :
    > fp1 0.000200
    > ------------------------
    > fp2 0.000300
    > difference is 0.000000
    >
    > which means the result is zero.


    One more thing I forgot to mention: Read section 14 of the C FAQ.
    <http://www.eskimo.com/~scs/C-faq/top.html>.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Nov 28, 2005
    #3
  4. yezi

    yezi Guest

    thanks, I will first read the FAQ .
    yezi, Nov 29, 2005
    #4
  5. >I have question with the data type problem. According to the C
    >standard. IEEE 754
    >
    >float 4 3.4x10-38E..3.4x10+38E
    >double 8 1.7x10-308E..1.7x10+308E
    >long double 12 ???
    >
    >The code function is (0.000200-0.000300)*(0.000200-0.000300)
    >suppose the result (difference ) should be the 10 (-8)
    >
    >difference I claim is double;
    >
    >But THe code result is :
    >fp1 0.000200
    >------------------------
    >fp2 0.000300
    >difference is 0.000000


    >which means the result is zero.


    You print out a value you expect to be 1.0e-8 with *six* digits
    after the decimal point, and you wonder why it shows as zero?

    Try printing the result with a format like %200.100f . You'll
    notice that the result isn't exact. None of the following numbers
    are exactly representable in binary floating point: 0.000200,
    0.000300, 1.0e-8. For that matter, *most* aren't unless they are
    exact integers. A few exceptions are: 0.5, 0.25, 0.75, 0.125,
    0.375, 0.625, 0.875 .

    Gordon L. Burditt
    Gordon Burditt, Nov 29, 2005
    #5
  6. "yezi" <> writes:
    > thanks, I will first read the FAQ .


    Please read this as well:

    <http://cfaj.freeshell.org/google/>

    (Thanks to Chris F.A. Johnson for setting up this page.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Nov 29, 2005
    #6
  7. Keith Thompson said:

    > "yezi" <> writes:
    >> thanks, I will first read the FAQ .

    >
    > Please read this as well:
    >
    > <http://cfaj.freeshell.org/google/>
    >
    > (Thanks to Chris F.A. Johnson for setting up this page.)
    >


    Just a nit...

    <li><a
    href="http://www.catb.org/~esr/faqs/smart-questions.html#bespecific">http://www.catb.org/~esr/faqs/smart-questions.html#bespecifichttp://en.wikipedia.org/wiki/Netiquette</a>

    is a bit longer than it should be.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Nov 29, 2005
    #7
  8. yezi

    jacob navia Guest

    yezi wrote:

    > Hi: All:
    >
    > I have question with the data type problem. According to the C
    > standard. IEEE 754
    >
    > float 4 3.4x10-38E..3.4x10+38E
    > double 8 1.7x10-308E..1.7x10+308E
    > long double 12 ???
    >
    > The code function is (0.000200-0.000300)*(0.000200-0.000300)
    > suppose the result (difference ) should be the 10 (-8)
    >
    > difference I claim is double;
    >
    > But THe code result is :
    > fp1 0.000200
    > ------------------------
    > fp2 0.000300
    > difference is 0.000000
    >
    > which means the result is zero.
    >
    > Why?
    >
    > Thanks
    > bin YE
    >

    Using lcc-win32 I type:
    #include <stdio.h>
    int main(void)
    {
    double f = 0.0002;
    double g = 0.0003;
    double delta = (f-g)*(f-g);
    printf("%e\n",delta);
    }

    D:\lcc\mc63\test> lc texact.c
    D:\lcc\mc63\test> texact
    1.000000e-008

    D:\lcc\mc63\test>

    You are using the wrong printf format.
    jacob navia, Nov 29, 2005
    #8
  9. yezi

    yezi Guest

    This is work with %e, I find the type declarion and printf in sort of
    complex . It should be matched very well then got u want .
    yezi, Nov 29, 2005
    #9
  10. yezi

    Default User Guest

    yezi wrote:

    > This is work with %e, I find the type declarion and printf in sort of
    > complex . It should be matched very well then got u want .


    What is?


    Brian

    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
    Default User, Nov 29, 2005
    #10
  11. "yezi" <> writes:
    > This is work with %e, I find the type declarion and printf in sort of
    > complex . It should be matched very well then got u want .


    Please read this:

    <http://cfaj.freeshell.org/google/>

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Nov 30, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven T. Hatton
    Replies:
    6
    Views:
    1,015
    Steven T. Hatton
    Aug 22, 2004
  2. Christian Seberino
    Replies:
    3
    Views:
    1,167
    Christian Seberino
    Feb 5, 2004
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    862
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,762
    Smokey Grindel
    Dec 2, 2006
  5. debolina
    Replies:
    0
    Views:
    802
    debolina
    Jun 16, 2011
Loading...

Share This Page