Take value from float number after dot - swift

How correctly extract second valued after coma in Float value.
Example:
var value = 5.435
And I would like to take value second value after coma, that is 3.
How to do this properly?

In case you want to handle both positive and negative values:
(Int)( abs(value) * 100 ) % 10
If you want to keep the sign, just remove abs

Maybe this: (int)( value * 100 ) % 10.

If its always going to be the third decimal, I would do it like this.
var value = 5.435
value *= 100
var digit = value % 10

Mod is an expensive operation, instead do
(Int(value * 100)) - (Int(value * 10) * 10)
In your scenario we get
(Int(5.435 * 100)) - (Int(5.435 * 10) * 10);
(Int(543.5)) - (Int(54.35) * 10);
(543) - (54 * 10);
(543) - (540);
3

Related

Why does order matter for Sinusoidal paths in Unity

just a quick question regarding possibly how Unity2D engine compile or runtime works, or maybe something I don't understand at all, so the following code works properly:
pos -= Time.deltaTime * moveSpeed * transform.right;
transform.position = magnitude * pos + axis * Mathf.Sin(Time.time * frequency);
However if I move the pos + axis (both are Vector3) then the pathing does not do what is expected, I was just wondering why this is the case. For example the following code would not work how I want it to:
pos -= Time.deltaTime * moveSpeed * transform.right;
transform.position = magnitude * Mathf.Sin(Time.time * frequency) * pos + axis;
If anyone has any insight I'd like to know.
Thank you.
Unity will resolve math equations following the pemdas order of operations. To clarify, it will handle everything in the order of:
Parathesis
Exponents
Multiplication / Division
Addition / Subtraction
Along with this, the order of operations are read left to right, so whatever appears on the left will be handled first, which is how the tie breakers of Addition / Subtraction and Multiplication / Division are handled.
In your example case, moving the variables as you have results in a completely different operation. For simplicity, I will substitute the vectors for whole numbers and just write out the multiplication as vector * vector and vector * scalar are just scaled vectors, so I can equally substitute all of them for ints.
pos = 5
axis = 3
Mathf.Sin(frequency * Time.time) = 2
magnitude = 12
Now substituting these values into your two equations:
12 * 5 + 3 * 2 (12 * 5 is handled first, next 3 * 2 and then 60 + 6 = 66)
12 * 2 * 5 + 3 (12 * 2 is handled first, next 24 * 5 and then 120 + 3 = 123)
Following the pemdas rule I explained above, the solutions would work out to be:
66
123
If you would like an explanation using vectors I can write one out.

What is wrong with my vector 3 speed-cap formula?

3D movement. If the player moves left-backwards, they will go at double the speed of any other direction. MoveDirection.x and z are equal to the respective analog stick directions, ranging from 1 to -1.
MoveDirection = Vector3(MoveDirection.x - (MoveDirection.z / 2 * MoveDirection.x), MoveDirection.y, MoveDirection.z - (MoveDirection.x / 2 * MoveDirection.z))
I thought that the results would be something like 0.5, 0, 0.5 if moving diagonally but that isn't the case.
Are you sure it doesn't also occur in the right-backwards direction as well? I'm thinking that this could likely be be related to how Godot handles arithmetic order. For example, if Godot does multiplication before division (like I think it does), then assuming we use whole values (1 or -1), then Godot would process the first Vector3 coordinate in your code this way::
Using - (MoveDirection.x - (MoveDirection.z / 2 * MoveDirection.x)
Substitute values for - (x,z)
FOR VALUES (1 , -1)
(1) - (-1 / (2 * 1))
(1) - (-1) / (2)
(1) - (-0.5)
= 1.5 <<<<<<----------- THIS IS YOUR PROBLEM (I THINK)
FOR VALUES (-1, 1)
(-1) - (1 / (2 * -1))
(-1) - (1) / (-2)
(-1) - (-0.5)
= -0.5
FOR VALUES (1 , 1)
(1) - (1 / (2 * 1))
(1) - (1) / (2)
(1) - (0.5)
= 0.5
FOR VALUES (-1, -1)
(-1) - (-1 / (2 * -1))
(-1) - (-1) / (-2)
(-1) - (0.5)
= -1.5 <<<<<<----------- THIS SHOULD ALSO BE A PROBLEM (I THINK)
If my math is wrong, please forgive me, it's 5:00AM where I am. If I am wrong, let me know and I'll delete this answer.
EDIT: It turns out arithmetic order doesn't even matter, it does the same thing with division first. My suggestion would be to find a way to subtract 1 from (or add 1 to) the affected results. Maybe something like: if (MoveDirection.x > 0) AND (MoveDirection.z < 0), subtract 1 from result.

Round up to the nearest .5 decimal in swift

I have a function that looks like this
bits = bits * 1.5
So if bits was 1 it would return 1.5 but if the function was run again it would return 2.25 however i would prefer if it were to just round up to 2.5 is that possible?
Maybe try the following function from globalnerdy.com:
In your case you could use bits = roundUp(bits, 0.5) after the multiplication by 1.5.
func roundUp(_ value: Double, toNearest: Double) -> Double {
return ceil(value / toNearest) * toNearest
}
You could do it by doing...
round(x * 3) * 0.5
So multiply by 3. Then round it to the nearest int. Then divide by 2.
So you have still multiplied by 1.5 but it gets rounded to the nearest 0.5
1.5 * 3 = 4.5
Rounded = 5
5 / 2 = 2.5
Which is what you wanted
Edit: if you want to always go up to the nearest 0.5 then use ceiling instead of round.

Objective C, math functions: round a Float32 with two or three decimals

I'm developing an iPhone app.
I want to round a Float32 number like this 3.124 to this 3.12. Or a number like 4.2258 to 4.226.
How can I do that?
I want to hold this value on a Float32 variable or on a float variable.
float x = 4.2258;
x = x * 100;
roundf(x);
x = x / 100;
That's one option. You can also use NSNumberFormatter to get what you want.
Which way do you want to round?
x = 0.01 * floorf(0.5 + 100.0 * x);

pi in Objective C

I keep getting error in my iPhone programing when I try to use pi. I'm trying
float pNumber = 100*cos(2 * pi * (days/23));
But i get errors that say:
_pi, referenced from
_pi$non_lazy_ptr
I saw somewhere on the internet to use M_PI and it compiles but I don't think it gives me the correct calculation.
When I try:
float pNumber = 100*cos(2 * M_PI * (15746/23));
I get 0
Thanks
The integer division probably needs to be coerced into a floating point one (cast one of the numbers to a double - or use the notation 23.0 to indicate that you want a floating point division).
Try printing out M_PI and see what it says (printf("M_PI = %16.9g\n", M_PI); in C).
Did you include the declaration for cos()? If not, it may be interpreted as a function returning an integer (#include <math.h> perhaps).
Example code (tested in C on Solaris 10 SPARC with GCC 4.3.3):
#include <math.h>
#include <stdio.h>
int main(void)
{
float pNumber = 100*cos(2 * M_PI * (15746/23));
printf("M_PI = %16.9g\n", M_PI);
printf("pNum = %16.9g\n", pNumber);
pNumber = 100*cos(2 * M_PI * (15746/23.0));
printf("pNum = %16.9g\n", pNumber);
return 0;
}
Example output:
M_PI = 3.14159265
pNum = 100
pNum = -77.5711288
C/C++ and hence Objective C/C++ does not promote integers to floats when doing normal division.
So in C/C++ the expression 15746/23 evaluates to 567, not to 567.71207 as you might naively expect.
C will promote integers to floats as necessary if one or other operand is a float, so all you need to do is use 15746.0 or 23.0 in your expression, ie change to
float pNumber = 100*cos(2 * M_PI * (15746/23.0));
The 100 will automatically be promoted because cos returns a float (actually a double, but I will ignorefloat/double percissions issues). The 2 is promoted to a float because M_PI is a float. And the 15746 is promoted to a float because 23.0 is a float.
However, it would not hurt to add the .0 to all the constants, ie:
float pNumber = 100.0*cos(2.0 * M_PI * (15746.0/23.0));
The problem is the integer division in the innermost part of the expression, which truncates the value (omitting the fractional part). One option, as mentioned, is to make every constant a floating point number, either by adding ".0" or "f" after it. Alternatively, you can omit the parentheses from the innermost expression entirely. Since M_PI is a floating point number, and multiplication in C is left-associative (meaning it proceeds from left to right) the first multiplication (2 * M_PI) will be promoted to a float, as will each successive multiply. Since cos() returns a float, pNumber will be assigned a float without having performed any integer division, hence no loss of precision. (Note: It's not usually wise to count on operator associativity or precedence, but in this case I'm just trying to demonstrate that it would in fact work.)
As far as the range of numbers you should expect to see, recall that the cosine (unmodified) ranges from -1 to +1, not 0 to 1, so you would actually see -100 to 100 (in theory). To get the correct range, you'd want to add 1, then multiply by 50.
Incidentally, the compile errors you get in the first case are because pi is not defined. The guidance to use M_PI is correct — for math constants, it's always smarter (and more consistent) to use what the system provides. If you're curious, on Leopard these constants are #defined in Math.h, lines 528-540. You can open the file by using File > Open Quickly... (Cmd-Shift-D) and typing "Math.h", or by double-clicking on M_PI in your code while holding down Command.
Why use a sinusoid in the first place ?
If the goal is to have a fonction ranging from 0 to 100 then 100 to 0 in 23 days, you could use:
// x ranges from 0 to 2
float x = (days % 23)/11.5;
// pNumber is a saw ranging from 0 to 100
float pNumber = 100 * abs(x - 1);
You can also replace the x by a cosine if you really want one, as 2*pi/23 ~= 0.273, you have
float x = 1 + cos((days % 23)*0.273);