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 6 years ago.
Improve this question
I'm looking for an approximation for Sine which is correct at x == 0 and x == 90 or x == pi/2 and within 5% otherwise.
I have no space for look-up tables.
You seem to be able to use Bhaskara I's sine approximation formula :
float x;
float sinx = 4 * x * (180 - x) / (40500 - x * (180 - x));
The error stays within 2%.
Very fast and can be optimized (by hand).
Since the derivative of a sin is cos which never goes above 1, and 5% is 1 / 20, a lookup table with 20 * pi / 2 = 32 points would satisfy your requirements. Are you sure you can't spare even 32 bytes of your RAM to store it?
The only way that I can think to do this is to use Taylor/Maclaurin Expansions.
These form a polynomial which represents an ever-improving approximation to a function.
In general you pick a value of the function about which you want to approximate via Taylor series.
For example around x = 0 radians sin(x) = x - x^3 / 3! + x^5 / 5!. The series is infinite and the more terms you include the closer to the true value you will get.
I suggest that you might have to form several of these Taylor expansions at "convenient" places (30, 60, 45, 90 degrees). And then use the function that you angle is closest to.
Found with a quick linear fit.
1.00003 x - 0.000312267 x^2 - 0.165537 x^3 - 0.00203937 x^4 + 0.010286 x^5 - 0.000961693 x^6
This maximum error is 3%.
Related
I have this piece of code written by somebody else that runs on a TI TMS320 Command Law Accelerator. So it's optimized in size and speed.
In order to get 1/x, the code always does something like this.
float32 y = __meinvf32(x);
y = y * (2.0f - y*x);
y = y * (2.0f - y*x);
I found this thread that propose something similar, but in my case, there is no clamping at the end.
Can seomebody help me understand what is the intent behind this?
Isaac Newton figured this out.
__meinvf32(x) gives an approximation of 1/x, say 1/x • (1+e), where e is some small relative error.
Let y = 1/x • (1+e). Then, when we calculate y • (2 − y•x), we have:
y • (2 − y•x) =
(1/x • (1+e)) • (2 − (1/x • (1+e))•x) =
1/x • (1+e) • (2 − (1+e)) =
1/x • (2 + 2e − (1+e) − e(1+e)) =
1/x • (2 + 2e − 1 − e − e − e2) =
1/x • (1 − e2).
Since e is small, e2 is even smaller. Thus, by calculating y • (2 − y•x) we get an estimate of 1/x that is closer than before; the relative error is only −e2 instead of e. Repeating this improves the estimate again (up to the limits of floating-point precision).
With some knowledge of bounds on the initial e, we can calculate how many repetitions are needed to get the estimate as close as desired to the correct result.
y = e + 1/x where e is some small error.
So (2.0 - y*x) is close to 1.0 and has the effect of reducing e on each pass.
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 6 years ago.
Improve this question
This is a question about theoretical computing. I have came through a question like below;
Consider a project with the following functional units :
Number of user inputs = 50
Number of user outputs = 40
Number of user enquiries = 35
Number of user files = 06
Number of external interfaces = 04
Assuming all complexity adjustment factors and weighing factors as average, the function points for the project will be;
The answer is 672. How is this calculated?
1. Typical complexity averages are as follows:
AVERAGE complexity weights = {4, 5, 4, 10, 7} for the 5 complexities respectively.
2. Typical Characteristic weights are as follows:
AVERAGE characteristic weight = 3.
3. Function point = FP = UFP x VAF
UFP = Sum of all the complexities i.e. the 5 parameters provided in the question,
VAF = Value added Factor i.e. 0.65 + (0.01 * TDI),
TDI = Total Degree of Influence of the 14 General System Characteristics.
Thus function points can be calculated as:
= (200 + 200 + 140 + 60 + 28) x (0.65 + (0.01 x (14 x 3))
= 628 x (0.65 + 0.42)
= 628 x (1.07)
= 672
Thus the function points for the project will be 672.
Checkout this article for a detailed walk-through into function-point calculations.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to get following result in SQL Query.
If number is 121 then query should return me 130
If number is 125 then query should return me 130
If number is 128 then query should return me 130
If number is 130 then query should return me 130
If number is 137 then query should return me 140
If number is 140 then query should return me 140
You don't even need the floor() function when you're dealing with integer math.
(num + 9) / 10 * 10
Or you can think of it as finding the tens complement and adding that to the original number.
num + (10 - num % 10) % 10
Use FLOOR function with some math, something like this
select floor((121 + 9) / 10) * 10; --130
SQL FIDDLE DEMO
You need ROUND(number, -1)
ROUND - Rounds a positive or negative value to a specific length and accepts three values:
Value to round
Positive or negative number
This data type can be an int (tiny, small, big), decimal, numeric, money or smallmoney
Precision when rounding
Positive number rounds on the right side of the decimal point
Negative number rounds on the left side of the decimal point
Truncation of the value to round occurs when this value is not 0 or not included
SqlFiddleDemo
SELECT
num
,[rounded] = ROUND(num, -1)
FROM (VALUES (121), (125), (128), (130), (137), (140)) AS tab(num)
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have a problem. I want to find factorial of big numbers.
Ex: 1555! = ?.
195! = ?.
My main problem is that I want to know the exact number of ending 0's of the factorial numbers.
I use the following formula:
(m!)^n = m! = 2*10^(n-1) + 2^2 * 10^(n-2) + ------- + 2^n.
with this I can solve the other factorials for number of ending 0's like this.
100!= 2*10^1 + 2^2*10^0 = 20+4 = 24
100! has 24 ending 0's as per this calculation.
But, then I got other problem,
Ex: For 95!
i) 95! = (100 - 5)! = 24 - 2*5^(1-1) = 24 - 2 = 22 => 95! has 22 0's.
ii) 95! = (90 + 5)! = 9*(2*10^0) + 2*5^0)= 18+2 = 20 => 95! has 20 0's.
this is my problem. By using the above formula I got two different answers and I am confused, I don't get the perfect answer so please help me to find it.
Thank you...
The number of trailing zeros in n! is the number of factors of 5 in the sequence 1, 2, ..., n. This is because a trailing zeros is the number of factors of 10 in the result, and 10 has a prime factorisation of 5 x 2. There's always more factors of 2 than 5, so the number of 5's gives the result.
The number of factors of 5 is... [n/5] + [n/25] + ... + [n/(5^k)] + ... where [ ] means round down (floor).
What should the code look like to compute this? Something like this perhaps.
int trailing_factorial_zeros(int n) {
int result = 0;
int m5 = 5;
while (n >= m5) {
result += n / m5;
m5 *= 5;
}
return result;
}
This is a bad question, probably belongs on Math site anyway. But here's a thought for you:
First 100! = 100 * 99!
99! = 99 * 98! and so forth until
1! = 1, and 0! = 1.
You want to know how many trailing 0's are in N! (at least that is how I understand the question).
Think of how many are in 10!
10! = 3628800
so there are two. The reason why is because only 2*5 = a number with a trailing 0 along with 10. So we have a total of 2. (5*4 has a trailing 0 but 4 is a multiple of 2, and besides, we only get to multiply individual numbers once)
It is a good bet, then, that 20! has 4 (it does).
It's now your job to prove (or disprove) that this pattern will hold, and then come up with a way to code it.
In his answer to this question, John Feminella says:
It's possible to do this sub-quadratically if you get really fancy, by
representing each integer as a bit vector and performing a fast
Fourier transform, but that's beyond the scope of this answer.
What is the asymptotically optimal way of solving the problem described in that question?
Suppose we have an array 1 2 4. We represent this array as a polynomial f(x) = x^1 + x^2 + x^4. Let's look at f(x)^2, which is
x^2 + 2 x^3 + x^4 + 2 x^5 + 2 x^6 + x^8
The number of ways to write n as the sum of two elements of the array is the coefficient of x^n, and this is true in general. FFT gives us a way to multiply polynomials efficiently*, so basically what we do is compute f(x)^3 and look at the coefficient of the target number S.
The reason this algorithm doesn't solve the 3SUM problem is that the efficiency of an FFT multiply depends on the degree of the resulting polynomial and thus that the array values lie in a small range.