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.
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 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
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to know the different techniques that are used for performing arithmetic operations on very large integers in C. One that I know of is using string to hold a number and define operations add, subtract etc. for it. I am not interested in using libraries, this question is purely for knowledge. Please suggest any other such methods/techniques used.
You can go as low level as representing your integers as an array of bytes, and do all the operations (like addition, subtraction, multiplication, division or comparison) just like a CPU does them, at word level.
The simplest algorithms are for addition and subtraction, where you simply add or subtract the digits in sequence, carrying as necessary.
Negative numbers can be represented in 2's complement.
For comparison, you just compare the high order digits until a difference is found.
For multiplication the most straightforward algorithm (and slowest) you can implement is repeated addition.
For division, things are a little more complicated than multiplication, see: http://en.wikipedia.org/wiki/Division_algorithm
A common application for this is public-key cryptography, whose algorithms commonly employ arithmetic with integers having hundreds of digits.
Check the OpenSSL BIGNUM documentation for this: https://www.openssl.org/docs/crypto/bn.html
You could use 3 linked lists, one for number A, one for number B and one for the result.
You would then read each digit as a character input from the user, make it an integer and and save it to a new node in the list, corresponding to the number you read at the moment.
And Finally you would just write as functions the operations for adding,subtracting etc.
In each you would follow their respective algorithm you learned at school, starting from the LSB node, going up to the MSB node, always keeping at mind the base powers of each number(1 node * 10^0, 2 node * 10^1, 3 node * 10^2, ...,n node * 10^n ).
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.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I have a lengthy calculation (polynomial of 4th degree with fixed decimals) that I have to carry out on a microcontroller (TI/LuminaryMicro lm3s9l97 [CortexM3] if somebody is interested).
When I use 32bit-Integers, some calculations flow over. When I use 64bit Integers the compiler emits an ungodly amount of code to simulate 64bit-multiplication on the 32bit-processor.
I am looking for a program into which I could input (just for example):
int a, b, c;
c = a * b; // Do the multiplication
c >>= 10; // Correct for fixed decimal point
c *= a*b;
where I could specify, that a and b would be in the range of [15000..30000] [40000..100000] respectively and it would tell me what sizes the integers need to not overflow (and/or underflow; I would possibly get a false positive there for the >> 10) in the specified domain, so that I could use 32bit-integers where possible.
Does something like this exists already or do I have to roll my own?
Thanks!
I think you have to roll your own. Implementing an extended sequence of muls and divs in fixed-point can be tricky. If fixed-point is applied without careful thought, overflow can happen quite easily. When implementing such a formula, I use a spreadsheet to experiment with the following:
Ordering of operations: muls require twice the number of bits on the left-hand side, i.e. multiplying two 22.10 numbers can yield a 44-bit result. Div operations reduce the number needed on the LHS. Strategically re-ordering the equation's evaluation, or even rewriting it (expanding, factoring, etc) can provide opportunities to improve precision.
Pre-computed scalars: along the same lines, pre-computing values may help. These scalars may not be need to be constant, since look-up tables may be used to store a collection of pre-computed values.
Loss of precision: is 10-bits of precision really needed at steps in the evaluation of the equation? Perhaps some steps need lower precision, leaving more bits on the LHS to avoid overflow.
Given these concerns (all of which are application-specific), optimal use of fixed-point math remains very much a manual exercise. There are good resources on the web. I've found this one useful on occasion.
Ada might be able to do that using range types.