I want to write a program that calculates expontiation using the Gauss' algorithm but if give input etc base=2,exp=50 i get as a result 0.0000.
#include<stdio.h>
float fastpower(int a,int b);
main()
{
int base,exp;
printf("Base:\n");
scanf("%d",&base);
printf("Exp:\n");
scanf("%d",&exp);
fastpower(base,exp);
system("pause");
}
float fastpower(int a,int b)
{
double result=1;
while (b>0)
{
if (b%2!=0)
result=result*a;
b=(b/2);
a=a*a;
}
printf("result is %lf\n",result);
}
Declare a as long (int64):
/*
compute a**b
*/
/* double fastpower(double a, int b) is even more better */
double fastpower(long a, int b) { /* double is more natural here: double result */
double result = 1.0;
while (b > 0) {
if (b % 2 != 0)
result *= a;
b /= 2;
a *= a; /* <- a is long to prevent overflow here */
}
/* You'd rather not output in functions */
printf("result is %lf\n", result);
return result; /* do not forget to return the result*/
}
But long could do overflow as well (e.g. 10**50); in this case use double for a
Related
I wrote a program to calculate the value of e^x by series and by library function.See the following:
#include<stdio.h>
#include<math.h>
long long fact(int x)
{
long prod=1;
int i=1;
if(x==0)
return 1;
else{
while(i<=x)
{
prod=prod*i;
i++;
}
return prod;
}
}
int main()
{
int i;
float x;
double sum=1;
for(x=1;x<20;x++)
{
for(i=1;i<=10;i++)
{
if(fact(i)!=0);
sum=sum+pow(x,i)/fact(i);
}
printf("by code e=%.15lf\t\t",sum);
printf("by libfnc e=%.15f\t",exp(x));
printf("quotient =%.15f\n",sum/exp(x));
}
}
The code works for smaller values like 1,2 but with the increase of the value of x the difference (here quotient) increases.That is my code no longer gives correct answer for higher values of x.
You need function prototype before calling it.
Add some ifs to check if you do not divide by zero.
scanf returns number of successfully scanned elements. Check for 1 in this case.
int fct = fact(2*i);
if(fct)
sum=sum+y/(double)fct;
else
{printf("DIVISION BY ZERO!!!!\n"); return 1;}
You will discover that int is to small for factorial function. Change it to double. Also, use double in all floating point operations.
double fact(int x);
int main()
{
double sum,y;
int x,i=0;
double fct;
while(scanf("%d",&x) == 1)
{ sum=0;
for(i=0;i<=N;i++)
{
y=pow(-1,i)*pow(x,2*i);
fct = fact(2*i);
if(fct != 0.0)
sum=sum+y/fct;
else
{printf("DIVISION BY ZERO!!!!\n"); return 1;}
}
printf("using Maclaurin's series %.10f and original cosx=%.10f",sum,cos(x));
}
}
double fact(int x)
{
double prod=1.0;
int i=1;
for(i=1;i<=x;i++)
prod=prod*i;
return prod;
}
https://godbolt.org/z/qsh4qh3d1
I tried following function (as suggested on these forums) to calculate power. However, it is causing program to hang up.
static long ipow(int b, int e) {
long r = 1;
while (e--) r *= b;
return r;
}
double cfilefn(int a, int b, int c) {
return (ipow(a, b) / (double)c);
}
cfilefn(2,3,4);
The function looks all right. Where is the error and how can it be solved?
The ipow function will misbehave if the second argument is a negative number: it will run for a while and have implementation defined behavior when e reaches INT_MIN. You should modify the test while (e--) r *= b; as:
static long ipow(int b, int e) {
long r = 1;
while (e-- > 0)
r *= b;
return r;
}
Note however that ipow will cause arithmetic overflow for moderately large values of e and since you want a double result from cfilefn, you should use double arithmetics for the power function:
#include <math.h>
double cfilefn(int a, int b, int c) {
return pow(a, b) / c;
}
I've tried changing variable types but this is still not working.
double power(double a, long long b){
while(b>1){
a *= a;
b--;
}
return a;
}
Your code won't run properly when b>2 because when you're doing a = a*a; the second time it won't be a^3 but a^4 and the next time it will be a^8 and so on.
The right code would be something like this below:
double power(double a, long long b){
double k = 1;
while(b>0){
k *= a;
b--;
}
return k;
}
You're changing a on every iteration. Say you call it like power(2, 3).
First you do 2 * 2 and assign this to a, which becomes 4.
Next iteration, you'll do again a * a which is 4 * 4. Just keep the result in a variable and don't change the arguments:
double power(double a, long long b){
double r = a;
while(b>1){
r *= a;
b--;
}
return r;
}
you are changing the variable a and it courses the defects. what you can do instead is
double power(double base, double exp)
{
double result = 1;
for(int i = 0; i < exp; i++)
{
result *= base;
}
return result;
}
The below returns too few arguments in the int main section on the fp and newton lines.
Can you explain this?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double f(double a, double b, double c, double x)
{
return pow(x,3)+a*pow(x,2)+b*x+c;
}
double fp(double a, double b, double c, double x)
{
return 3*pow(x,2)+(2*a*x)+b+0*c;
}
double newton(double a, double b, double c, double x)
{
return x - (f(a,b,c,x)/fp(a,b,c,x));
}
int main()
{
double a,b,c,x1,x2;
int i;
char *input = getenv("QUERY_STRING");
sscanf(input, "coeff1=%lf &coeff2=%lf &coeff3=%lf &x=%lf", &a, &b, &c, &x1);
if (fp(x1)==0.0)
{
printf("No convergence.\n");
return 0;
}
for (i=0;i<100;i++)
{
x2=newton(x1);
if (fabs(x1-x2)<= pow(10,-10)) break;
x1=x2;
}
printf("iteration = %d\n", i);
printf("x= %lf\n", x1);
return 0;
}
Just what the error says! You haven't passed enough arguments:
This prototype:
fp(double a, double b, double c, double x) {
means you need to pass four arguments, like:
fp(x1, what, about, these);
The same goes for newton.
Also, regarding if (fp(x1)==0.0) - While floating point zero values can be compared with each other (zero is excatly zero), remember that floating-point on computers is not exact. For that reason, you should always compare against some epsilon value:
#define EPSILON 0.0001 // salt to taste
#define ABS(x) ( ((x)<0) ? -(x) : x )
#define FP_EQUALS(x,y) (ABS(x-y) < EPSILON)
//if (d == 0.0) {
if (FP_EQUALS(d, 0.0)) {
// d is "zero"
}
//if (a == b) {
if (FP_EQUALS(a, b) {
I have written a small piece of code to calculate quadratic equations, but if the discriminant is negative, i wanted it to write that there are no real numerical values for this quadratic equation.
To make this happen, I had to call a function with a fourth parameter of 0, which i think , i have no idea why, would be a bad programming practice ? Is it the case or am i just being too picky of my code ? Thank you. (The reason I'm asking this is because i dont want to pick up some bad habits early on in my programming 'career').
Here's the code.
#include <stdio.h>
#include <math.h>
#include <string.h>
double quadratic_equation(double a, double b, double c, double d);
int main(void)
{
char command[20];
int i;
printf("Enter your command: ");
fgets(command, 20, stdin);
for (i = 0; i < 20; i++) {
if (command[i] == '\n') {
command[i] = '\0';
break;
}
}
if (strcmp(command, "quadratic equation") == 0) {
double a, b, c, x;
printf("Enter A: ");
scanf("%lf", &a);
printf("Enter B: ");
scanf("%lf", &b);
printf("Enter C: ");
scanf("%lf", &c);
x = quadratic_equation(a, b, c, 0); // THIS PIECE HERE MIGHT BE BAD PRACITCE ?
if (x == 0) {
printf("There are no real numerical values to this quadratic equation.");
}
else {
printf("------------\n");
printf("x1 = %.2f\n", quadratic_equation(a, b, c, 1));
printf("x2 = %.2f", quadratic_equation(a, b, c, -1));
}
}
return 0;
}
double quadratic_equation(double a, double b, double c, double d) {
double discriminant, x, insideroot;
insideroot = ((b*b) - (4*a*c));
if (insideroot < 0) {
return 0;
}
discriminant = sqrt(insideroot);
x = (-b + (d * discriminant)) / (2 * a);
return x;
}
Thank you very much for your help :d !
This certainly is bad practice. Since the roots of a formula a, b, and c an be any double you do need some sort of passing.
I would suggest a parameter that is a pointer to an int. If the pointer is NULL it is ignored, otherwise it will be set to 1 or 0 depending whether a real root exists:
double quadratic_equation(double a, double b, double c, int *root_exists) {
double discriminant;
discriminant = ((b*b) - (4*a*c));
if (discriminant < 0) {
if (root_exists != NULL) *root_exists = 0;
return 0.0;
}
x = (-b + sqrt(discriminant)) / (2 * a);
if (root_exists != NULL) *root_exists = 1;
return x;
}
A more rigorous approach is this:
typedef struct {
int num_roots;
double roots[2];
} quadratic_roots_t;
quadratic_roots_t quadratic_equation(double a, double b, double c) {
quadratic_roots_t roots;
double d;
d = b*b - 4*a*c;
if (d < 0.0) {
roots.num_roots = 0;
} else if (d == 0.0) {
roots.num_roots = 1;
roots.roots[0] = -b / (2 * a);
} else {
roots.num_roots = 2;
roots.roots[0] = (-b - sqrt(d)) / (2 * a);
roots.roots[1] = (-b + sqrt(d)) / (2 * a);
}
return roots;
}
I'd say it's not great to do. What you could do is something like this:
int quadratic_equation(double a, double b, double c, double *root_a, double *root_b) {
double discriminant = ((b*b) - (4*a*c));
if (discriminant < 0) {
return -1;
}
if (root_a != NULL) {
*root_a = (-b + sqrt(discriminant)) / (2 * a);
}
if (root_b != NULL) {
*root_b = (-b - sqrt(discriminant)) / (2 * a);
}
return 0;
}
Then you could call that like so:
double root_a;
double root_b;
int ok = quadratic_equation(a, b, c, &root_a, &root_b);
if (ok < 0) {
// It wasn't OK. Print out an error.
} else {
// It was OK. Print out the results.
}
Note that you should also check other error cases in the function and return -1 for them as well. E.g. a being zero.
Consider using the return value to indicate whether everything worked, and passing an array to the function to receive the return values:
enum QE_Status { QE_OK = 0, QE_NON_QUADRATIC, QE_COMPLEX_ROOTS, QE_NULL_POINTER };
enum QE_Status quadratic_equation(double a, double b, double c, double *r)
{
double discriminant;
if (r == 0)
return QE_NULL_POINTER;
if (a == 0.0)
return QE_NON_QUADRATIC;
discriminant = (b * b) - (4 * a * c);
if (discriminant < 0)
return QE_COMPLEX_ROOTS;
discriminant = sqrt(discriminant);
r[0] = (-b + discriminant) / (2 * a);
r[1] = (-b - discriminant) / (2 * a);
return QE_OK;
}
You can extend the system to handle numerical instability (because b*b is almost equal to 4*a*c, or because a is very small, etc).
The calling code can then be:
double a, b, c, x[2];
if (quadratic_equation(a, b, c, x))
...oops, something went wrong...
Or:
switch (quadratic_equation(a, b, c, x))
{
case QE_OK:
...print or use results in x...
break;
case QE_NON_QUADRATIC:
case QE_COMPLEX_ROOTS:
...print appropriate error message about user's data...
break;
case QE_NULL_POINTER:
...Oops - programming error...
break;
}
I would certainly call it bad practice because the code is very unclear.
Firstly, you are calling the function three times, when once should be enough.
I'd consider returning/filling a list in your quadratic_equation() function instead of returning the roots one by one.
This would also allow you to determine if there are real roots -- if there aren't, just return an empty list.
As a whole, this would be much more elegant than your current solution, and it would eliminate the need for checking whether there are any solutions beforehand.