I know a similar question has been asked before with C#
Difference between 2 numbers
But I need to know if objective-c provides some function to find the difference between 2 numbers, (2 NSIntegers, to be specific)
For example the difference between:
100 and 25 is 75
-25 and 100 is 125
100 and -25 is also 125
-100 and -115 is 15
//I know I'm using the same example as the previous
Any help is very much appreciated
I think basic math is something that every programming language does: abs(num1 - num2) will work.
Remember that abs() works for integers and will automatically round any non-integer arguments in addition to performing the absolute value function. My programs tend to be full of fractional data so I end up using fabs() a lot.
Related
It looks like I can hack floor and ceiling into a meta function for an arbitrary round, but is there a standard way to do:
round[3.1415;2] = 3.14
from the core functions?
Edit: This is problem I'm trying to solve:
I have unadjusted daily stock data, and I'm trying to identify splits and filter them out. It's further complicated by decimalisation artefacts (a split might not be perfectly 2:1 because prices have minimum tick sizes).
This is possible via the use of .Q.f, this does however return the output as a string.
Using this we can round 3.14159 to 2 decimal places like so:
q) .Q.f[2;] 3.14
"3.14"
q)round:{"F"$.Q.f[y]x}
q)round[3.1415;2]
3.14
There's no standard way because in kdb you shouldn't ever really need to round floats. What's the reason to round floats? If it's just for pretty-printing displays then you can use a method like Matthew suggests, but underlying kdb logic shouldn't need it.
If you must, you can use floor/ceiling as you've already figured out or you can use the built-in nearest-rounding that casting to interger/long achieves. E.g.
q)d:{("j"$x*y)%x}
q)d2:d[100]
q)d3:d[1000]
q)
q)a:5?10.0
q)
q)d2 a
3.92 0.81 9.37 2.78 2.39
q)d3 a
3.915 0.812 9.368 2.782 2.392
But even here you should be aware that this is completely fictional rounding because you're still using floats and still subject to floating point fuzziness. If I increase the precision value those same results above are:
q)\P 18
q)
q)d2 a
3.9199999999999999 0.81000000000000005 9.3699999999999992 2.7799999999999998 ..
q)d3 a
3.915 0.81200000000000006 9.3680000000000003 2.782 2.3919999999999999
The only way to have a truly concrete number of decimal places is to store the whole numbers as ints/longs and separately store the decimals as ints/longs
I am getting wrong answer to the following problem:
Given a non-negative number n, print True if n is within 2 of a multiple of 10, else print false. For example 22 is within 2 of a multiple of 10 (the multiple here is 20) and 23 is not within 2 of a multiple of 10 (it is within 3 of multiple 20).
Do you see anything wrong? Because certainly I do not.
This is my code:
closest_multiple = round(num/10)*10
return (abs(closest_multiple - num) <= 2)
Thanks to the comment of #user202729, the answer is:
apparently it has to do with the accuracy of the floating point representation, which applies to most languages. Perhaps is not that good to work with fp numbers then in this case... 9999999999999999.0 in fp64 would be 10000000000000000
Is there a way to force MATLAB to use single precision as default precision?
I have a MATLAB code, whose output I need to compare to C code output, and C code is written exclusively using floats, no doubles allowed.
Short answer: You can't.
Longer answer: In most cases, you can get around this by setting your initial variables to single. Once that's done, that type will (almost always) propagate down through your code. (cf. this and this thread on MathWorks).
So, for instance, if you do something like:
>> x = single(magic(4));
>> y = double(6);
>> x * y
ans =
4×4 single matrix
96 12 18 78
30 66 60 48
54 42 36 72
24 84 90 6
MATLAB keeps the answer in the lower precision. I have occasionally encountered functions, both built-in and from the FileExchange, that recast the output to be a double, so you will want to sprinkle in the occasional assert statement to keep things honest during your initial debugging (or better yet put the assertion as the first lines of any sub-functions you write to check the critical inputs), but this should get you 99% of the way there.
You can convert any object A to single precision using A=single(A);
The Mathworks forums show that
in your case: system-specific('precision','8'); should do it. Try this in the console or add at the top of your script.
If the following code is executed MATLAB makes a mistake. Can someone verify this?
floor([0.1:0.1:2]/0.01)
So what is the 129 doing here??
ans = 10 20 30 40 50 60 70 80 90 100 110 120 129 140 150 160 170 180 190 200
It is a floating point rounding error because of the colon-generated vector.
Like Rasman said, if you do:
floor((0.1:0.1:2 + eps) / 0.01)
There will be no rounding errors.
However, based on how the colon operator works, I suggest that you do the same calculation like this:
floor([(1:20)/10] / 0.01)
[Edit: following Rasman's comment, I will add that the latter approach works for negative values as well, while adding eps sometimes fails]
The bottom line is that it is better using the colon-operator with integer numbers to minimize rounding errors.
It is probably doing a floating point calculation resulting in an inexact value of 129.99999999999999... something instead of 130. and then you floor it to 129.
it's a rounding approximation brought on by the array construction. The solution would be to add eps:
floor([0.1:0.1:2]/0.01+ eps([0.1:0.1:2]/0.01))
I have a QBASIC program that basically consists of formulas and constants, and I want to translate the formulas and constants into a C++ programm. Since the formulas are not rocket science and the program is well documented, I have no problem translating the program, although I have not used or seen QBASIC before.
However, there is an initialization of a variable that reads abc(15) = 9.207134000000001D-02, and I am not sure how to interpret the D-02. I guess I should translate it like abc[15] =0.09207134...., but I'd like to verify if this is correct.
If I recall correctly D-02 means times ten raised to the power minus 2.
So 8.309618000000001D-02 = 8.30961800000000 x 10^(-2)
which is roughly 0.08309618
I also think the D means the type of the number is a double.
EDIT: It's been ages since I wrote any QBASIC code
Yes he is right the D means that the number is a double and the -2 after the D means it is multiplied by 10 to the power of negative 2 which means it is 0.08309618 to the precision of qbasics double precision numbers which is 52 or 54 bits If I remember corectly