When I solve tan(3.14) with calculator,it gives 0.0548582.
When I solve with Math.tan the answer is 0.0014.
When I solve with Math.atan the answer is 1.26.
Which tan method to use so that i get answer like my calculator gives and how?
package testclass;
public class TestClass {
public static void main(String[] args) {
System.out.println(Math.atan(3.14));
System.out.println(Math.tan(3.143));
}
}
First of all: Under no circumstance should tan(3.14) be +0.0014; you've pasted it with the wrong sign.
This is not a programming question.
The three functions you use are different functions:
atan is arcustangens, i.e. the inverse to tan, so it's completely unrelated.
your calculator seems to use degrees
your Math.tan function uses radians. 3.14 is "pretty close, but a little smaller" than pi, and sin(3.14) is thus pretty close, but a little bigger than 0, cos(3.14) is thus pretty close, but a little smaller (in absolute terms) than -1, so tan(3.14) is a little less than 0.
EDIT: tan x = sin x/cos x, to explain my calculations above.
Related
This question already has answers here:
Calculating factorial of large numbers in C
(16 answers)
Closed 2 years ago.
Consider problem of calculating factorial of a number.
When result is bigger than 2^32 then we will get overflow error.
How can we design a program to calculate factorial of big numbers?
EDIT: assume we are using C++ language.
EDIT2: it is a duplicate question of this one
As a question with just algorithm tagged. Your 2^32 is not an issue because an algorithm can never have an Overflow error. Implementations of an algorithm can and do have overflow errors. So what language are you using?
Most languages have a BigNumber or BigInteger that can be used.
Here's a C++ BigInteger library: https://mattmccutchen.net/bigint/
I suggest that you google for: c++ biginteger
If you can live with approximate values, consider using the Stirling approximation and compute it in double precision.
If you want exact values, you'll need arbitrary-precision arithmetic and a lot of computation time...
Doing this requires you to take one of a few approaches, but basically boils down to:
splitting your number across multiple variables (stored in an array) and
managing your operations across the array.
That way each int/element in the array has a positional magnitude and can be strung together in the end to make your whole number.
A good example here in C: http://www.go4expert.com/forums/c-program-calculate-factorial-t25352/
Test this script:
import gmpy as gm
print gm.fac(3000)
For very big number is difficult to stock or print result.
For some purposes, such as working out the number of combinations, it is sufficient to compute the logarithm of the factorial, because you will be dividing factorials by factorials and the final result is of a more reasonable size - you just subtract logarithms before taking the exponential of the result.
You can compute the logarithm of the factorial by adding logarithms, or by using the http://en.wikipedia.org/wiki/Gamma_function, which is often available in mathematical libraries (there are good ways to approximate this).
First invent a way to store and use big numbers. Common way is to interpret array of integers as digits of a big number. Then add basic operations to your system, such as multiplication. Then multiply.
Or use already made solutions. Google for: c++ big integer library
You can use BigInteger for finding factorial of a Big numbers probably greater than 65 as the range of data type long ends at 65! and it starts returning 0 after that. Please refer to below Java code. Hope it would help:
import java.math.BigInteger;
public class factorial {
public factorial() {
// TODO Auto-generated constructor stub
}
public static void main(String args[])
{
factorial f = new factorial();
System.out.println(f.fact(100));
}
public BigInteger fact(int num)
{
BigInteger sum = BigInteger.valueOf(1);
for(int i = num ; i>= 2; i --)
{
sum = sum.multiply(BigInteger.valueOf(i));
}
return sum;
}
}
If you want to improve the range of your measurement, you can use logarithms. Logarithms will convert your multiplication to additions making it much smaller to store.
factorial(n) => n * factorial(n-1)
log(factorial(n)) => log(n) * log(factorial(n-1))
5! = 5*4*3*2*1 = 120
log(5!) = log(5) + log(4) + log(3) + log(2) + log(1) = 2.0791812460476247
In this example, I used base 10 logarithms, but any base works.
10^2.0791812460476247
Or 10^0.0791812460476247*10^2 or 1.2*10^2
Implementation example in javascript
I've found many definitions on how to compute the power of an interval, where the power is an integer, but I'd like to find a formula for computing the power more generally.
In other words, I'd like to implement something like the 'pow' method below:
class Interval {
public final double min;
public final double max;
public Interval pow(Interval i) {
return new Interval(..., ...);
}
}
In Interval Arithmetic, the standard four operators are relatively straight-forward (read: available on Wikipedia), transcendental functions are not much harder, and integer powers of intervals are not difficult... But interval powers of intervals are throwing me for a loop.
I haven't been able to find any open-source libraries in any language that implement this generalized power function.
I'd appreciate any complete answers, suggestions, and references.
i have read that some machine can't express exaclty floating point number for example 1.1
let's take code
float x=0.1;
do{
x+=0.1;
printf("%f\n",x);
} while(x!=1.1);
this code never finished how can i make that code finish? maybe convert it to double or?
For numerical problems, it is common to specify an epsilon of accuracy:
bool within_epsilon(float x, float y, float e) {
if (abs(x - y) > e) {
return false
} else {
return true
}
}
The epsilon you choose will change your accuracy, and the epsilon you can choose is dependent on your floating point implementation: Machine epsilon.
For example, compare within an acceptable margin. I.e.
while (abs(x-1.1)>0.001);
Doubles will have the same issue, just with more precision. Some languages also offer you rational types, where you can specify a number as the fraction 1/10, or fixed point data types.
In this case, checking "<" will do the trick:
float x=0.1;
do{
x+=0.1;
printf("%f\n",x);
} while(x<1.05);
In general, you should test against an "epsilon". Look here for further information.
Work in fixed point, for that kind of task.
The decimal type for example might help. It's not the solution for all problems though.
If you want to do code precisely like you are saying then you want to use a type like decimal (where available) which is a base 10 floating point implementation rather than a base 2.
Further reading: http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems and http://en.wikipedia.org/wiki/Decimal_floating_point
I have some function,
int somefunction( //parameters here, let's say int x) {
return something Let's say x*x+2*x+3 or does not matter
}
How do I find the derivative of this function? If I have
int f(int x) {
return sin(x);
}
after derivative it must return cos(x).
You can approximate the derivative by looking at the gradient over a small interval. Eg
const double DELTA=0.0001;
double dfbydx(int x) {
return (f(x+DELTA) - f(x)) / DELTA;
}
Depending on where you're evaluating the function, you might get better results from (f(x+DELTA) - f(x-DELTA)) / 2*DELTA instead.
(I assume 'int' in your question was a typo. If they really are using integers you might have problems with precision this way.)
You can get the numerical integral of mostly any function using one of many numerical techniques such as Numerical ordinary Differential Equations
Look at: Another question
But you can get the integration result as a function definition with a library such as Maple, Mathematica, Sage, or SymPy
The only algorithm that I know for this problem is the Newton's method (make a guess, then improve it until it's good enough).
Any other ideas (use any language you prefer)?
PS: Of course I don't have any use case for this, I'm just researching it for academic reasons.
There is always the John Carmack method, which is a highly efficient variation upon the Newton method.
Several can be found here.
You may want to check algorithms in Methods of computing square roots.
This is fast C Log base 2 implementation of the Newton's Method:
double sqrt(const double x)
{
union
{
int i;
double x;
} u;
u.x = x;
u.i = (1<<29) + (u.i >> 1) - (1<<22);
return u.x;
}