So here is a program that is supposed to calculate an approximation to the value of pi if you take enough terms into the sum which is mathematically described in the following program and calculates the expression of the root, you get a value that gets closer and closer to the value of pi the more terms you have.
#include <stdio.h>
#include <math.h>
main()
{
int j, terms;
double sum, precision, pi;
printf("How many terms: "); scanf("%d", &terms);
for(j=1;j<=terms;j++)
sum+=1/(j*j);
pi = sqrt(6*sum);
printf("Pi: %lf.\n", pi);
}
But there is something making it go wrong here and I can't quite figure out what.
sum+=1/(j*j);
I thought the mistake might be in that line because all others look fine,thinking at first maybe the computer isn't counting decimals.I'm unsure.But my question is: What is it in this code that makes it malfunction?And how do I fix it?
This performs integer division:
1/(j*j);
try this:
sum+=1.0/(j*j);
If j*j might overflow, do this
sum+=1.0/((double)j*j);
Related
I have made a simple addition function, but getting wrong summation values, sorry if it is a naive thing to look for. But really want to know why is this happening. I want an alternative solution for this. Thanks in advance!
I am using notepad++ to write the code, and gcc compiler in the windows command prompt to execute the code.
#include <stdlib.h>
#include <stdio.h>
float addit(float num1, float num2)
{
float sumit;
sumit = num1 + num2;
return sumit;
}
int main()
{
float num1, num2;
float answer = 0.000000;
printf("Enter first number: ");
scanf("%f", &num1);
printf("Enter second number: ");
scanf("%f", &num2);
answer = addit(num1, num2);
printf("The summation is: %f", answer);
return 0;
}
The expected output of the addition of 2345.34 and 432.666 is 2778.006.
But, after execution it shows 2778.006104.
Windows cmd window showing execution result as 2778.006104
Welcome to the wonderful world of floating point values! Consider this: between 0 and 1, there are infinitely many numbers. There's 0.1, and there's 0.01, and there's 0.001, and so on. But computers do not have infinite space, so how are we supposed to store numbers? Our solution was to limit the precision of the numbers we stored. As others have mentioned, java's float data type only has 6 or 7 digits of accuracy (in base 10). Because of this, you cannot necessarily guarantee that the result of a mathematical operation on floats will be 100% accurate. You can only be sure that the first 6-7 digits of the result will be.
I'm writing a program that calculates the value of the normal distribution function given to me here:
The program is supposed to ask the user for the mean μ and the Standard Deviation σ for the normal distribution showed above. The program then asks for N values of x and then asks for each value of x, one by one. After each value x it writes out the corresponding value of the function.
This is my code so far:
#define _USE_MATH_DEFINES
#include <stdio.h>
#include <math.h>
int main() {
int j;
double u, stddev, N, result, x;
printf("Enter u and stddev for the Normal Distribution:\n");
scanf("%lf %lf",&u, &stddev);
printf("Enter how many values of x (N) for the Normal Distribution:\n");
scanf("%lf",&N);
for (j=0; j<N; j++) {
printf("Enter a value for x: \n");
scanf("%lf",&x);
result = ((1)/(stddev*sqrt(2*M_PI)))*exp(-(1/2)*((x-u)/stddev)*((x-u)/stddev));
printf("%.6lf\n", result);
}
}
I'm basically done but the answers the program is giving me are wrong when compared to my answers from my calculator. For instance, when I make N = 3 no matter what I put for the 3 values of x, the answer it gives me for each are the same when they shouldn't be.
So I know my issue lies in this line of code:
result = ((1)/(stddev*sqrt(2*M_PI)))*exp(-(1/2)*((x-u)/stddev)*((x-u)/stddev));
Am I just writing the function wrong in the program? I must be, for it not to work.
Your code is clean and functional. Good work so far.
You were correct about which line needed changing - The issue is that using integer division results in an integer, so 1 / 2 results in a value of 0. This can be remedied by using a single double value: 0.5, or by dividing using doubles 1.0 / 2.0.
result = ((1)/(stddev*sqrt(2*M_PI)))*exp(-(0.5)*((x-u)/stddev)*((x-u)/stddev));
I tested out your code after making these changes try it online, and they match up perfectly with the formula on wolfram alpha, as well as what you said in the comments.
For example:
μ=2, σ=3, x=7 results in 0.033159
μ=3, σ=7, x=0 results in 0.051991
μ=4, σ=4, x=4 results in 0.099736
I know this problem has been on the internet for a while but i cant seem to find how to stop my program from rounding the 3rd decimal.
the answer output is 4524.370 and should be 4524.369
also, i know my equations are stupid and could be simplified but im lazy
//Tanner Oelke CSE155E
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(void){
double v, t; //base variables
double sq1, sq2; //calculation variable for square root
printf("Please enter the given air temperature in Fahrenheit:");
scanf("%lf", &t);
//unessecary equations but it works
sq1=(57*t+297);
sq2=(sq1/247);
v=1086*sqrt(sq2);
printf("%.3lf\n", v);
return 0;
}
With an input of "70.0" the result is 4524.369754... which displays as "4524.370" - What OP gets.
With an input of "69.999975" the result is 4524.369002... which displays as "4524.369" - what OP wants.
If OP expects "70.0" to result in "4524.369", then some minor adjustment to the formula is needed. The precision of double is at least 10 significant digits and often is 15+. Even doing this in float then f(70.0)--> 4524.370.
Else OP has the wrong expectation.
Response to OP's comment:
"to shorten the decimal place to 3 spots without rounding". Hmmm seems strange to want this:
// properly rounded result
printf("%.3lf\n", v);
// shorten to 3 places without rounding
double v3 = floor(v*1000.0)/1000.0;
printf("%.3lf\n", v3);
Thanks a lot people for your help so far but I made a big mistake I need the derivation of a function at a specific point!
I have to calculate the first derivation of a function and I really have no clue how to get there. If I just had to calculate it for a function with just a X^1 I would know how to but I'm really stuck here.
Old Stuff:
A function can look like 2*x^2+1.
The method has to look like this: double ab(double (f)(double),double x)
and my professor gave us the hint that we might should use the function:
(f(x0+∆x)−f(x0))/((x0+∆x)−x0).
Sorry for my bad English and thanks for any kind of hint or tip in advance.
this sample will get you started :
#include<stdio.h>
#include <stdlib.h>
float func(float x)
{
return(2*x*x + 1);
}
int main(){
float h=0.01;
float x;
float deriv, second;
printf("Enter x value: ");
scanf("%f", &x);
// derivative at x is the slope of infinitely small
// line of the function
deriv = (func(x+h) - func(x))/h; // I assumed the length to be h
//for second derivative you can use:
second = (func(x+h) - 2*func(x) + func(x-h))/(h*h);
printf("%f\n", deriv);
return 0;
}
The idea is approximate the first derivative of f() at x with the slope of the secant line through the points (x, f(x)) and (x+∆x, f(x+∆x)).
The Wikipedia article should get you started.
#include <stdio.h>
#include <math.h>
int fib();
int scan;
int main() {
scanf("%d", &scan);
printf("%d\n", fib());
scanf("%s");
return 0;
}
int fib() {
return floor((pow(1+sqrt(5)/2, scan)-(-pow(1-sqrt(5)/2, scan)))/sqrt(5));
}
I'm pretty new to programming with C and decided to try and calculate any number in the Fibonacci series. I based it off of my lua script here. I'm at a loss of what I've done wrong, could someone give me some insight?
You have the formula wrong. You want fib to be:
int fib() {
return round((pow((1+sqrt(5))/2, scan)-(-pow((1-sqrt(5))/2, scan)))/sqrt(5));
}
instead. You were missing parenthesis around the 1+sqrt(5) and 1-sqrt(5) terms and were using floor instead of round, which was underestimating the fibonacci numbers in my tests. (This mostly has to do with low precision in the pow function. The seventh fibonacci number, 13, came out to 12.969)
You also probably want to change
scanf("%s");
to
char tmp;
scanf("%c", &tmp);
Since the way you have it incorrectly omits an argument.
Hope this helps!