% operator giving strange result in swift ios - ios

I am familiar with Mathematics and what the % operator (modulo) gives us for certain values. However, I am following along a Swift code lecture, and the instructor wants to return a value somewhere between 0 and half of the height of the view. He sets up the equation as:
var offSet = arc4random() % UInt32(self.frame.size.height / 2)
I must be missing something. Wouldn't arc give a number between 0 and 1, and then performing % on the height (roughly 700 pixels) would always give 0. Yet each time the code is run it offsets a random amount somewhere between 0 and half the height of the screen. If I change % to * the program crashes.
Ideas?

Read the arc4random() man page
"arc4random() function returns pseudo-random numbers in the range of 0 to (2**32)-1"
So your random number will be something potentially large, then the modulo will bring it into the range of 0 - (height/2 - 1)

Related

How to perform "complex" calculations in LaTeX/TikZ

I expect this question has already been answered a thousand times, but I just cannot find a solution to my issue =(
I want to calculate some values in LaTeX (TikZ), some of which are lengths (e.g. 10 pt), some are not. As long as calculations are of very simple forms like a*b+c everything is fine, but if I need brackets like (a+b)*(c+d) LaTeX complains. Furthermore, if I have nested definitions, these are not solved as expected. Example:
\def\varA{1+2}
\def\varB{10}
% I want to calculate (1+2)*10 or 10*(1+2), respectively.
\def\varC{\varA*\varB} % evaluates to 21
\def\varD{\varB*\varA} % evaluates to 12
So basically my question is: What is the correct (or recommended) way to do calculations in LaTeX?
As a more realistic example, here is something I actually want to do and just can't:
% total height of my TikZ image
\def\myheight{20ex}
% five nodes shall be drawn as a stack
\def\numnodes{5}
% but there shall be some space at the top (like a 6th node)
\def\heightpernodeA{\myheight / (\numnodes + 1)} % fails whenever I want to use it
% workaround?
\def\numnodesplusone{\numnodes + 1}
\def\heightpernodeB{\myheight / \numnodesplusone} % fails for the reason explained above
Unfortunately I can not redefine \numnodes to be 6 as I use the variable for various calculations.
best regards
You can use \pgfmathsetmacro for more complex calculation. You'll need to remove the unit in myheight though:
% total height of my TikZ image
\def\myheight{20}
% five nodes shall be drawn as a stack
\def\numnodes{5}
% but there shall be some space at the top (like a 6th node)
\pgfmathsetmacro\heightpernodeA{\myheight / (\numnodes + 1)}
You can add the unit back when you use it: \draw (0,0) -- ({\heightpernodeA ex},{1});

XCode iOS UISlider - Custom Scaling Min and Max

I want to have a different scale using a slider for the Min and Max. 0 should be in the middle, but I want to have a scale 0-50 for max and -200-0 for min.
Is that possible? How could I achieve that?
I don't think that this is possible out of the box.
But what you can do is, e.g. setup the slider in a way that for min it has 0 and for max 1000. Now you will have to check in which part of the part, user left the slider. If it's in the lower half, you will have to normalize those values to the desired range, the same for the upper range. The exact middle could be a bit tricky because you will have more points in the slider than in the ranges you are providing, but I guess with some fiddling around you can do this. :)
Hope that helped :)
I believe this is quite possible.
Here is my idea of how you can do it
Take a slider with values -200 to 200
For values > 0. Divide value by 4. (i.e. 200 / 4 = 50)
This will make your positive range from 0 to 50 but negative range from -200 to 0
Similarly while setting value to slider in positive manner you can multiply it by 4.

How random is arc4random (mac os x)? (or what am I doing wrong?)

I'm playing with an optimized game of life implementation in swift/mac_os_x. First step: randomize a big grid of cells (50% alive).
code:
for(var i=0;i<768;i++){
for(var j=0;j<768;j++){
let r = Int(arc4random_uniform(100))
let alive = (aliveOdds > r)
self.setState(alive,cell: Cell(tup:(i,j)),cells: aliveCells)
}
}
I expect a relatively uniform randomness. What I get has definite patterns:
Zooming in a bit on the lower left:
(I've changed the color to black on every 32 row and column, to see if the patterns lined up with any power of 2).
Any clue what is causing the patterns? I've tried:
replacing arc4random with rand().
adding arc4stir() before each arc4random_uniform call
shifting the display (to ensure the pattern is in the data, not a display glitch)
Ideas on next steps?
You cannot hit period or that many regular non-uniform clusters of arc4random on any displayable set (16*(2**31) - 1).
These are definitely signs of the corrupted/unininitialized memory. For example, you are initializing 768x768 field, but you are showing us 1024xsomething field.
Try replacing Int(arc4random_uniform(100)) with just 100 to see.

Having issues getting a percentage of a number in iOS 7.1 applicaion xcode 5.1

In my iOS 7.1 application that I'm trying to develop, I need to figure out percentages of a specific number which is entered in a UITextField. It would appear when executing the code I get the wrong percentage of that number entered.
I've tried two different ways to get the require percentage answer I'm looking for, however it keeps giving the wrong answer.
Below here is the two methods that I've tried.
For example I want to get 72% of 250. If you were to do this on a calculator or better yet a excel spreadsheet I get the right answer 250 x 1 - 72% = 70. This is the correct answer I want
Method1 (.m file) Not working
Values that are set the the specific .text parameters:
Entered in the UITextField _linuxOracleOnDiskw_oRTC.text = 250
Value set to UITextField _formulaNumber.text = 1
Percentage Value set to _linuxOracle_Percent.text = 0.72
_linuxOracleOnDiskwithRTC.text = [NSString stringWithFormat:#"%.2f", ([_linuxOracleOnDiskw_oRTC.text doubleValue])*([_formulaNumber.text doubleValue])-([_linuxOracle_Percent.text doubleValue])];
When executed I the answer or vale that gets entered in the UITextField _linuxOracleOnDiskwithRTC.text is 249.28. This is wrong should be 70
Second method tried is as follows:
float linuxOracleOnDiskw_oRTC = [_linuxOracleOnDiskw_oRTC.text floatValue];
_linuxOracleOnDiskwithRTC.text = [NSString stringWithFormat:#"%.2f", (linuxOracleOnDiskw_oRTC * 1 - 72/100.0f )];
If someone can tell me what I maybe doing wrong and point me in the right direction with calculating percentages of a specific number entered in a UITextField I would be extremely GREATFUL.
Don't omit your brackets when performing calculations with code. For "250 x 1 - 72%" to get 70, you need to do this:
250 x (1 - 0.72) = 250 x 0.28 = 70
Formulas in () will be calculated first, followed by multiplication and division (whichever first), then followed by addition and subtraction (whichever first). So, insert your brackets appropriately.
Your formula to calculate the percentage is incorrect. One correct way to calculate percentages is
250 / 100 x 72
This gives you 72% of 250. The result of that isn't 70, btw, but 180. If you want 70, then you don't want 72% but 28% (or 100% - 72%):
250 / 100 x (100 - 72)

Which datatype should be used for long float values in iOS? [duplicate]

This question already has answers here:
Objective-C - How to increase the precision of a float number
(3 answers)
Closed 10 years ago.
In my application I am just dividing 50 by 3 I want to store the exact result value of this.
If I use float it gives 16.666666 and if i use double then it gives 16.666667.
Actually,I am creating three labels inside a frame by dividing the height of the frame I am deciding the height of each label. so I f i do not get exact value it creates a gap between labels.if if i pass 60 then it works fine because 60/3 results 20 but if I pass 50 then there is a gap.
If you want to make your frame divide into three equal-height areas, then the height of your frame in pixels needs to be divisible by three. You can't display fractional pixels, they are not divisible; each height as measured in pixels needs to be an integer number.
The only way to store an "exact" value would be to create a class called "Rational" (or similar) and store the numerator and denominator of the fraction as separate ivars. Floats and doubles (or any literal computer representation for that matter) cannot store rational numbers with an infinite number of decimal places or transcendental real numbers.
The way to use the "Rational" class would be to store the numerator and denominator, and then apply the appropriate maths to these values (if you wish to propagate "exactness" through the program). The slightly easier way would be to display the rational number as numerator and denominator but use the float/double approximation for the underlying mathematics.
float t= (float)50/3;
long double t1= (long double)50/3;
NSLog(#"%.30f %.30LF",t, t1);
produced output "16.666666030883789062500000000000 16.666666666666666666088425508008".
I would suggest you to go with long double which is not exact but precise enough.

Resources