round to the nearest multiple of 1/16 in C - c

Please let me know how to round a decimal number like 0.53124 to a nearest multiple of 1/16 which is 0.5. And similarly, when you round 0.46875 we must get 0.5. Thanks

floor(0.53124 * 16 + 0.5) / 16
floor(0.46875 * 16 + 0.5) / 16
floor(x * 16 + 0.5) / 16

I suppose, that you can multiply by 16, call round(double x) and divide by 16. noob code:
double x;
x=x*16;
x=round(x);
x=x/16;
and the one line code:
double x;
x=round(x*16)/16;

C Code:
answer = (int) ((x + 1.0/32.0) * 16) / 16.0;
Python verification:
>>> int(((.53124 + 1.0/32) * 16)) / 16.0
0.5
>>> int(((.46875 + 1.0/32) * 16)) / 16.0
0.5
>>>

Related

C arithmetic precedence

I stumbled upon a question about arithmetic precedence in a test and I cannot wrap my head at all around its answer.
float x = 5 % 3 * + 2 - 4.5 / 5 * 2 + 2;
My "understanding" right now is that multiplication must take place first before division and modulus, yet when I try using that approach, the answer is 6.55 instead of 4.20. I tried playing around with the expression (adding brackets here and there), and it turns out that 5 % 3 takes place first before everything else. I just don't understand why since, according to the precedence table I was provided, that shouldn't be the case. Could someone clear this up for me?
Please refer to the documentation here.
The precedence of multiplication, division and remainder operators are higher than those of sum and subtraction.
When multiplication, division or remainder operators go one after another, then they are left-associated, meaning they will be executed one by one in the given order.
In your example 5 % 3 will be performed first, then the multiplication (by whatever number there is), then the division 4.5 / 5, then multiplication of the result by 2, and only after all that will the sum and subtraction be performed.
Your C code:
x = 5 % 3 * + 2 - 4.5 / 5 * 2 + 2;
First, unary plus and unary minus has the highest precedence:
x = 5 % 3 * (+ 2) - 4.5 / 5 * 2 + 2;
Second, multiplication, division, and remainder have the same precedence, associated from left to right:
x = ((5 % 3) * (+ 2)) - ((4.5 / 5) * 2) + 2;
Last, addition and subtraction have the same precedence, associated from left to right:
x = ((((5 % 3) * (+ 2)) - ((4.5 / 5) * 2)) + 2);
Now we evaluate the expression:
x = (((2 * (+ 2)) - ((4.5 / 5) * 2)) + 2);
x = (((2 * 2) - ((4.5 / 5) * 2)) + 2);
x = ((4 - ((4.5 / 5) * 2)) + 2);
x = ((4 - (0.9 * 2)) + 2);
x = ((4 - 1.8) + 2);
x = (2.2 + 2);
x = 4.2;
you can refers this link for more detail
http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm

Understanding the math behind the code

I have a really basic and simple question but I am having problems with understanding this C code.
#define POLYNOMIAL(x) \
(((((3.0 * (x) + 2.0) * (x) - 5.0) * (x) - 1.0) * (x) + 7.0) * (x) - 6.0)
This definition is for this polynomial: 3x5+2x4-5x3-x2+7x-6
How can I convert this polynomial into the form shown in the #define? Is there any trick for this?
Your polynomial:
3x5 + 2x4 - 5x3 - x2 + 7x - 6
Can be rewritten successively:
(3x4 + 2x3 - 5x2 - x + 7) · x - 6
((3x3 + 2x2 - 5x - 1) · x + 7) · x - 6
(((3x2 + 2x - 5) · x - 1) · x + 7) · x - 6
((((3x + 2) · x - 5) · x - 1) · x + 7) · x - 6
This an expanded, or unrolled, Horner's Method loop. If the coefficients were expressed as an array:
double polynomial[] = { -6, 7, -1, -5, 2, 3 };
Then, the polynomial could be evaluated with this function:
double horners (double poly[], int terms, double x) {
double result = 0;
while (terms--) {
result = result * x + poly[terms];
}
return result;
}
Just add parenthesis and decrease out powers inside until you get to the last one, like this:
(3x^5)+(2x^4)-(5x^3)-(x^2)+7x-6
((3x^4)+(2x^3)-(5x^2)-x+7)x-6
(((3x^3)+(2x^2)-5x-1)x+7)x-6
((((3x^2)+2x-5)x-1)x+7)x-6
((((3x+2)x-5)x-1)x+7)x-6

Floating point operation in c

We know that in C, the floating point range is from 1.xxxx * 10^-38 to 3.xxxx *10^38 for single precision.
On my lecture slides there is this operation:
(10^10 + 10^30) + (-10^30) ?= 10^10 + (10^30 + -10^30)
10^30 - 10^30 ?= 10^10 + 0
I'm wondering why 10^10 + 10^30 = 10^30 in this case?
What I thought is, since the range of FP can go down to 10^-38 and up to 10^38, there shouldn't be an overflow, so`10^10 + 10^30 shouldn't end up being 10^30.
As said in the comment to your question the part which store the digits is finite. It is referred to as the significand.
Consider the following simple 14 bit format:
[sign bit] [ 5 bit exponent] [ 8 bit significand]
let 'bias' be 16, ie if the exponent is 16 it is actually 0 (so we get a good range or +/- powers)
and no implied bits
so if we have numbers greater than 2^8 apart like 2048 and 0.5
in our format:
2048 = 2^11 = [0][11011][1000 0000]
0.5 = 2^-1 = [0][01111][1000 0000]
when we add these numbers we shift the exponent so that they have the same decimal places. A decimal analogy is:
5 x 10 ^ 3 + 5 x 10 ^ -2 => 5 x 10^3 + 0.00005 x 10 ^ 3
so the siginifcand cant hold 12 places:
2 ^ 11 + 0.000000000001 x 2 ^ 11 = 1.000000000001 x 2 ^ 11
so it rounds back to 2 ^ 11
The essence is the notion of significant digits. It's roughly 7 decimal digits for IEEE754 float. If we use hypothetical decimal floating point numbers with 7 significant digits, the calculation is done in this way:
10^10 + 10^30 == 1.000 000 * 10^10 + 1.000 000 * 10^30
== (0.000 000 000 000 000 000 01 + 1.000 000) * 10^30 (match the exponent part)
=> (0.000 000 + 1.000 000) * 10^30 (round the left operand)
== 1.000 000 * 10^30
== 10^30
Note however that the matching operation and the rounding operation are done as a single step, ie. the machine can never deal with 0.000 000 000 000 000 000 01 * 10^30 which has too many significant digits.
By the way, if you conduct experiments on floating point arithmetics in C, you may find %a format specifier useful (introduced in C99.) But note that printf always implicitly converts float arguments to double.
#include <stdio.h>
int main() {
float x = 10e10, y = 10e30;
printf("(%a + %a) == %a == %a\n", x, y, x+y, y);
return 0;
}
http://ideone.com/WeXe22

Rounding down decimal numbers in SQL Server 2008

I read all rounding functions of T-SQL like Round, Floor, and Ceil, but none of them has rounded down decimal numbers correctly for me.
I have 2 questions:
How to round down a decimal number (3.69 ==> 3.5)?
How to round up the last 3 digits of an integer (e.g. 142600 ==> 143000)?
1) select CAST(FLOOR(2 * 3.69) / 2 AS decimal(2, 1)) handles the first case - courtesy of an answer to a similar question on SQL Server Forums, which I adapted and quickly checked.
Note that if the numbers you are rounding to the nearest 0.5 could be bigger (e.g. 333.69 => 333.5), be sure to specify more decimal precision when you cast (e.g. select CAST(FLOOR(2 * 3.69) / 2 AS decimal(10, 1))), or you could get an overflow error:
Msg 8115, Level 16, State 8, Line 1
Arithmetic overflow error converting numeric to data type numeric.
Extra precision will not affect the bottom-line result (i.e. select CAST(FLOOR(2 * 3.69) / 2 AS decimal(10, 1)) and select CAST(FLOOR(2 * 3.69) / 2 AS decimal(2, 1)) both yield 3.5); but it is wasteful if the numbers you are rounding will always be smaller.
Online references with examples are available for T-SQL FLOOR, CAST, and decimal to help.
2) select ROUND(142600, -3) handles the second case.
A similar online reference is available for T-SQL ROUND.
As per #J0e3gan 's anwser, Sql Server's Round allows rounding to the nearest powers of 10 using the length parameter, where length is 10^(-length), e.g.
length = 0 : 10 ^ 0 = nearest 1
length = 3 : 10 ^ -3 = nearest .001
length = -3 : 10 ^ 3 = nearest 1000
etc
However, in general, with a simple 1-based rounding function - e.g. (Sql Round with Length=0) to round to an arbitrary value of "nearest N" - with the formula:
round(X / N) * N
e.g. nearest 100
select round(12345 / 100.0, 0) * 100.0 -- 12300
select round(-9876 / 100.0, 0) * 100.0 -- -9900
select round(-9849 / 100.0, 0) * 100.0 -- -9800
... Nearest 0.5
select round(5.123 / 0.5, 0) * 0.5 -- 5.000
select round(6.499 / 0.5, 0) * 0.5 -- 6.500
select round(-4.499 / 0.5, 0) * 0.5 -- -4.50
... Nearest 0.02
select round(5.123 / .02, 0) * .02 -- 5.12
select round(-9.871 / .02, 0) * .02 -- -9.88
etc
Remember that the type used for the divisors must be numeric / decimal or float.
The Oracle/PLSQL FLOOR function returns the largest integer value that is equal to or less than a number.
eg:
FLOOR(7.9)
Result: 7
FLOOR(30.29)
Result: 30
FLOOR(-7.9)
Result: -8

What does "linear interpolation" mean?

I often hear the term "linear interpolation" in context with animations in WPF. What exactly does "linear interpolation" mean? Could you give me an example where to use "linear interpolation"?
Linear means lines (straight ones).
Interpolation is the act of finding a point within two other points. Contrast this with extrapolation, which is finding a point beyond the ends of a line.
So linear interpolation is the use of a straight line to find a point between two others.
For example:
*(5,10)
/
/
/
/
*(0,0)
You can use the two endpoints with linear interpolation to get the points along the line:
(1,2)
(2,4)
(3,6)
(4,8)
and linear extrapolation to get (for example):
(1000,2000)
(-1e27,-2e27)
In animation, let's say you have a bouncing ball that travels from the (x,y) position of (60,22) to (198,12) in 10 seconds.
With an animation rate of 10 frames per second, you can calculate it's position at any time with:
x0 = 60, y0 = 22
x1 = 198, y1 = 12
frames = 100
for t = 0 to frames:
x = (x1 - x0) * (t / frames) + x0
y = (y1 - y0) * (t / frames) + y0
Those two formulae at the bottom are examples of linear interpolation. At 50% (where t == 50):
x = (198 - 60) * (50 / 100) + 60
= 138 * 0.5 + 60
= 69 + 60
= 129
y = (12 - 22) * (50 / 100) + 22
= -10 * 0.5 + 22
= -5 + 22
= 17
and (129,17) is the midpoint between the starting and ending positions.
E.g. when you want a storyboard to move an element from one position to another using a fixed speed, then you'd use linear interpolation between the start and end positions.

Resources