long double help

This is a discussion on long double help within the C Programming forums, part of the General Programming Boards category; I am having trouble using these long doubles, I can not get them to work properly at all, I keep ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    1

    long double help

    I am having trouble using these long doubles, I can not get them to work properly at all, I keep getting 1 for sum1 and 0 for sum2. I was just curious could give me a hint at where I am going wrong with this.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int
    main(void)
    {
        long double sum1, sum2, pi;
        int i, j;
    
        sum1 = 0;
        sum2 = 0;
    
        for (i = 1; i <= 99; i+=4)
            sum1 = sum1 + (1 / i);
    
        printf("%Lf\n", sum1);
    
        for (j = 3; j <= 99; j+=4)
            sum2 = sum2 + (1 / j);
    
        printf("%Lf\n", sum2);
    
    
        pi = 4 * (sum1 - sum2);
    
        printf("pi = %Lf\n", pi);
    
        return 0;
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Use floating point division:
    Code:
    sum1 = sum1 + (1.0L / i);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. expected primary expression
    By mju4t in forum C Programming
    Replies: 2
    Last Post: 03-27-2007, 06:59 PM
  2. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 08:13 AM
  3. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 05:54 PM
  4. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 05:20 PM
  5. Quick, Partiotion, and Insertion Sort
    By silicon in forum C++ Programming
    Replies: 0
    Last Post: 05-18-2005, 08:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21