Rounding a float to the nearest .5 [duplicate] - ios

This question already has answers here:
Rounding a number to the nearest 5 or 10 or X
(13 answers)
Closed 9 years ago.
I need to round a float to the nearest .5, for example:
1.1 = 1
1.5 = 1.5
1.6 = 1.5
1.8 = 2
and so on.
I really don't know how to do it. Any help would be greatly appreciated.

For positive numbers Multiply by 2, add 0.5, truncate, divide by 2.

Related

why numbers moved out of my Seaborn heatmap? [duplicate]

This question already has answers here:
matplotlib/seaborn: first and last row cut in half of heatmap plot
(10 answers)
Closed 3 years ago.
sns.heatmap(metric, annot=True ,fmt='g', cmap ="Blues")
plt.title('Confusion matrix')
plt.ylabel('Actual label')
plt.xlabel('Predicted label')
I'm trying to plot my heatmap, but my numbers look very ugly!
Have you tried corr on dataset? something as follows
sns.heatmap(df.corr(),cmap='coolwarm',annot=True)

Objective-C: most efficient way to find the quadrant of an angle in Objective-C [closed]

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 3 years ago.
Improve this question
What is the most efficient way to find the quadrant of an angle given in Objective-C (assuming that the boundary angles 0, 90, 270, 360 etc. all fall within a quadrant) ?
You don't need any magic functions.
Full circle contains 2*Pi radians and 4 quadrants.
So just divide angle by Pi/2 and make floor rounding to get 0..3 quadrant numbering (add 1 if 1..4 is needed)
Python example. Note that integer modulo operation % 4 provides "angle normalisation" so function works with large and negative angles
(Swift % does not work similar according to table here, so you might need to make something like return ((floor(2.0 * angle / math.pi) % 4 + 4) %4)
import math
def quadrant(angle):
return math.floor(2.0 * angle / math.pi) % 4
print(quadrant(math.pi/4))
print(quadrant(math.pi/4 + math.pi))
print(quadrant(math.pi/4 - math.pi))
0
2
2

What does .f mean in CGSize in Objective-C? [duplicate]

This question already has answers here:
"f" after number
(10 answers)
Closed 7 years ago.
This must be in documentation somewhere but I cannot find it. What does the .f mean when defining rectangles using CGSizeMake as in CGSizeMake(200.0f, 100.0f);?
.f means that value is float.
if you write directly 1.0 it is initialized as double.
to use less space 1.0f is better.

Trigonometric functions in Swift [duplicate]

This question already has answers here:
Value of sine 180 is coming out as 1.22465e-16
(4 answers)
Closed 7 years ago.
I'm beginner developer for iOS. I use some online tutorials to learn Swift and now I'm trying to develop my own calculator. There is task to down "sin" and "cos" buttons by my own, which would return sine or cosine function for entered value.
Of course, there is sin() and cos() functions in the Swift, but I've found, that it returns values in radians, not degrees. I did search and found code, smth like that
func sind(degrees: Double) -> Double {
return sin(degrees * M_PI / 180.0)
}
which I implemented in my code. Now everything looks fine, buttons returns correct values. But there is sine of 180 degrees is 0 and when I enter 180 in my calculator and press "sin" button it returns another value. Same for cosine of 90 degrees, should be 0 but returns another value.
Could you please explain how possible to fix it? Full code at github: https://github.com/senator14/firstcalculator.git
The problem with sine and cosine functions is that M_PI is an irrational number is approximately defined as 3.14159265358979323846264338327950288 which means that it has some error.
One possible solutions to your problem is having the ranges of input form -PI/2 to PI/2. This reduces the error of approximation. The following changes your range to -90 to 90 degrees.
sin(((fmod($0, 360) > 270 ? fmod($0, 360) - 270 : ((fmod($0, 360) > 90) ? 180 - fmod($0, 360) : fmod($0, 360))) * M_PI / 180.00)) }
Reference from here

Best way to place icons on a circle [duplicate]

This question already has answers here:
Calculating the position of points in a circle
(13 answers)
Closed 9 years ago.
I want to place some menu icons on a circle so that it almost looks like a clock. Depending on the amount of icons, the space between them may vary. Whats the best way to put each of these icons in the form of a circle?
Mathematics.
If you know how many items you have then you can find the angle between them because you know there are 360 degrees (2pi radians) in a circle. Choose a start point and then use
x = centerX + r * cos(angle)
y = centerY + r * sin(angle)
Using the center point of the circle, the radius and the angle (in radians), increasing the angle for each item.

Resources