Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I´m programming in c and I'm using Netbeans IDE.
I'm trying to calculate the length of the sides of two triangles from their vertexes. However, when I try to use pow to calculate a square root, I get an error. I´ve included and when I use for example pow(2,2), it works. It's just this specific example that doesn't work. I've checked the parentheses like five times and they seem correct to me.
my code looks like this
(Don't mind the other variables, so far I'm just working with a1, a2, b1, b2 and c.)
You are using (1/2) as exponent in your call to the function pow. This is integer division and the result of 1/2 is 0. So this will not give you the square root.
Use 0.5 instead as exponent in the call to pow.
(1/2) will give you an integer value. and you need double.
you can use one of the following ways to avoid this situation.
(1.0/2) // (double/int) = double
// or (1/2.0)
((double)1/2) // 0.50000
// or ((double)1/(double)2)
Useful links :
C - Type Casting
Type Casting - C Programming
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 months ago.
Improve this question
In C, I am trying to figure out why my expression isn't always outputting what I am expecting it to. The expression is as follows:
z = (528*x*8)/(y*1.0e6)
In a calculator, this expression always gives me the expected result, but when ran in the program it doesn't always do as I expect it to. I don't remember the exact types of all of the variables, but I'm pretty sure that they are all unsigned ints. X and Y can both be pretty large numbers on their own. So, I'm wondering if it is possible that an integer overflow is happening before the expression is fully evaluated?
I'm wondering if it is possible that an integer overflow is happening before the expression is fully evaluated?
Yes, that is very possible.
For example, if x is 2000000, the subexpression 528*x*8 will overflow. Theoretically it would evaluate to 8448000000, but that's a 33-bit number. On most machines, type int is either 16 or 32 bits. (Probably 32 on yours, but still not big enough to hold a 33-bit number like 8448000000.)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
I have tried so many ways to calculate using this binomial theorem but I still couldn't find one:
The value of x and n is given for example b=0.5 and n=8
I know for the factorial we have to use loop but the numerator part is a little bit tricky.
Obviously I know how to code for (1+b)^n, but the question is still asking for the coding for binom theorem.
For example if the value of x is 0<x<1 and n is any positive integer, what will the value of (1+x)^n will be using the binomial theorem?
I understand that you know how to calculate the left side of the equation in programming.
I understand that you also know how to program the right side, apart from the problem that it is an infinite loop; but you want it to end at some point and have a result.
By the math theory ending early means a wrong result.
But in programming you will have problems with restricted precision of floating point math anyway. So you can take shortcuts to solve your problem.
In the comments you find recommendations how to do the calculation of each step efficiently. I will only focus on the end condition.
Write a loop calculating more and more precise steps.
End the loop when a freshly calculated (intermediate) result is the same as the previous one. With floating point representation having restricted precision that will sooner or later happen and the result will be within only one "minimal rounding" of the correct result.
Note:
In order to avoid the restricted precision getting in the way at the wrong place, I recommend to calculate the parts (as described in the recommendation in comments) in double and the intermediate results (those you compare for the loop condition) into a float variable.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I've written a program in C that iteratively solves Laplace's equation on a square domain by successive over-relaxation, but although it was working perfectly a few days ago, I've come back to it and now it's doing really weird things.
The program involves creating two 7x7 arrays, one to hold the values of phi (the dependent variable in Laplace's equation) and one to hold the residuals from the successive over-relaxation. The array of residuals is filled with zeroes initially because I thought that was probably a good idea, but thinking about it now it shouldn't make much difference either way.
The problem is that certain values in the array (specifically R[4][6]) jump around randomly to enormous numbers, even though I've set them to zero and then not touched them. I know this because I put another line in to output the value of the residual at (4,6) to the screen after each iteration. This problem is causing my method of checking for convergence to fail, and also somehow causing the final solution (plotted as a surface using gnuplot) to look correct except for small peaks and valleys in the surface.
I'm coding in C, Dev-C++ 4.9.9.2 is the compiler I'm using (the files are definitely saved as C files and not C++ ones too), outputting the data into a DATA file which comes up in notepad when opened, and finally using gnuplot to produce the 3D contour plots that are my results. I'm working on windows 7.
I've put the code here: https://gist.github.com/anonymous/8220425 and you need to enter 1.35 for alpha when the program asks you for it.
If anyone could help at all I would be very grateful!
printf("Residual at (4,6)=%lf\n",R[4,6]);
You mean:
printf("Residual at (4,6)=%lf\n",R[4][6]);
The issue here is that R[4,6] basically means R[6] (4,6 means: evaluate 4, then evaluate 6), which is some sort of pointer rather than a double. So it will print random-looking values depending on where your array has been allocated in memory.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I don't want an answer in the form of code, but I do need to know how to start working on this problem. I have just begun learning C language and during the learning process, I am stuck with this confusing question. The question is as follows:
Write a function to compute the distance between two points and use it to develop another function that will compute the area of the triangle whose vertices are *A(x1,y1), B(x2,y2), and C(x3,y3).* Use these functions to develop a function which returns the value 1 if the point (x,y) lines inside the triangle ABC, otherwise returns a value 0.
Please suggest me the appropriate coding for this problem in C language and kindly include comments for better understanding.
I know both the formulas by the way.
Approach this problem, like all other coding problems, in increments. Your problem statement states pretty much what you need to do:
first, write a routine to compute the distance between two points. You need to find the formula to do this. Probably easiest to use double variables, be sure to read about how to specify the format statement for printf so that it can print double variables.
next, find the formula to use in order to compute the area of a triangle.
the last step is the hardest, and you will need to use everything you learned in steps one and two. Again, dig out your calculus or algebra book and see how this is done and then write code to do the same thing. It is interesting to see how a math description gets translated to computer code, they are not the same thing and unless you write a lot comments in your code it will be difficult to see how they do the same function.
good luck.
PS. when defining a function that returns a double variable, code:
double distance_calc(double x1, double x2, double y1, double y2)
{
double computed_value;
// do the formula and compute: computed_value = etc.
return computed_value;
}
Hope this helps.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm just beginning and I don't know how to change floating point form to exponential form.
A float is a float and has no other representation other than it's binary representation in memory. But you can change the way you print it to the console.
This can be done by specifying it in the printf function.
see printf
What you need is printf("%.2e",myfloat)
Those two "numbers" are simply the result of formatting the same floating point number in two different ways. No number conversion or casting is involved.
If you are concerned about internal representation - don't worry, it's all the same under the hood.
If you are going to print x = 1692.75 in desired form, use printf("%2.2e\n", x);
They are one of the same thing.
If you wish to print it see the manual page for printf
You don't have to change the internal representation of the float, which is binary and has nothing to do with what you see when you print the value.
If you just want to print your float you can you the printffamily of functions:
printf("%.2e", 1692.75);
should do the trick.