473,387 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

sum calculate

36
need to calculte the sum of SIGMA(-1)^n*(1/(2n+1)) from n=0 to n=N

Expand|Select|Wrap|Line Numbers
  1.     for (i=0; i<N; i++)
  2.     {
  3.         X1=(-1)*(1/(1+2*i));
  4.         X=X-X1;
  5.     }
  6.  
  7.     printf("%lf", X);

how to do the trick for + - + - + ........ ?

TNX .......
Aug 8 '09 #1
6 3597
weaknessforcats
9,208 Expert Mod 8TB
Use an "if" inside the loop that tests i % 2. Use the true part of the "if" for your + and the "else" for your -.
Aug 8 '09 #2
sedaw
36
still have a problem the program printin " 1 ".

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. void main()
  3. {
  4.     double X0,X=0 ;
  5.     int i ;
  6.     for (i=0; i<5; i++)
  7.     {
  8.         X0= 1/(1+2*i) ;
  9.         if (i%2!=0)
  10.         {X0=-X0;}
  11.         X=X+X0;
  12.     }
  13.  
  14.     printf("%lf", X);
  15.     getchar();
  16. }
  17.  
whats wrong ?

TNX ....
Aug 8 '09 #3
Banfa
9,065 Expert Mod 8TB
2 problems, the first trivial %lf is for the type long double not double. For double use %f. However there is a good chance that on your platform long double and double are the same size so that is probably not actually causing a visible problem.

However

X0= 1/(1+2*i);

i is an integer and all the constants are integers so the calculation is done in integer arithmetic and then converted to a floating point (double).

In integer arithmetic all fractions are dropped at every stage so for instance

1/2 = 0

the fraction 0.5 is dropped.

In fact in general 1/n where n is an integer can have 3 values and 1 exception.

If n == 0 then you get a divide by zero exception, you can't divide by zero.
If n == 1 then 1/n = 1
If n == -1 then 1/n = -1
For any other n 1/n =0 because it is fractional and the fraction gets dropped.

In you case 0 <= i < 5 resulting values of n of 1, 3, 5, 7, 9. So when i = 0 x0 = 1 and in all other cases x0 = 0 giving your result.

The trick is not to do the arithmetic in integer arithmetic but to use floating point arithmetic. You can do this by making an one of the constants or variables used a floating point by either using a double constant (put on a decimal place) or by casting the variable.
Aug 8 '09 #4
sedaw
36
you help me alot .
thank you very much ! (-:
Aug 8 '09 #5
Sorry to ask. I'm new in programming. Which language did you use, is it c++ or java
Aug 9 '09 #6
JosAH
11,448 Expert 8TB
@Awaltii
Look at the name of the forum where this was posted: C/C++ now guess again.

kind regards,

Jos
Aug 9 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Phil Powell | last post by:
Relevancy scores are normally defined by a MySQL query on a table that has a fulltext index. The rules for relevancy scoring will exclude certain words due to their being too short (minimum...
1
by: Building Blocks | last post by:
Hi, All I need is a simle calculate form script which contains this: A script that can handle text input, radio buttons, checkboxes, and dropdowns. Each one of these variables will contain a...
2
by: Gįlos Zsuzsa | last post by:
Hi all, I need to calculate Formulas. For example: dim strFormula as string="25*(12-6)" I ned a DotNet Function to calculate this Formula (25*(12-6)=150): dim dblValue as double = 150
1
by: Robert de Ridder | last post by:
I use an asp page to display pages, where the dynamic content is in an iframe. To calculate the height of the iframe I use the calcheight function. However when trying to do this with external...
4
by: Jan Szymczuk | last post by:
I'm creating an MS Access 2000 database where I have a number of people entered using simple basic fields, Surname: SMITH Forenames: John DoB: 09/09/1958 Age:...
1
by: Ladislau S. | last post by:
Dear reader, I am an occasional user of MS Access 2000 running on Windows 98. My hobby is ship model building so I made a database for things that I want to buy. After two strokes I bin unable...
6
by: charliewest | last post by:
Can someone pls point me to or recommend the easiest way to calculate someoneĀ“s age using the TimeSpan object, in .NET CF? IsnĀ“t there a simple way to use the TimeSpan object to calculate the...
3
by: Libber39 | last post by:
Hi everyone, Have a query on how to calculate the amount of weeks and days contained in a number in an access query. ie: the difference in days between 2 dates amounts to 17 days. I want to now...
0
by: SuzK | last post by:
I am trying to calculate in VBA in Access 2002 a moving average and update a table with the calculations. Fields in my WeeklyData table are Week Ending (date) ItemNbr (double) Sales Dollars...
6
by: rrstudio2 | last post by:
I am using the following vba code to calculate the median of a table in MS Access: Public Function MedianOfRst(RstName As String, fldName As String) As Double 'This function will calculate the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.