I'd like to get a random number between two small decimal numbers.
Between maybe 0.8 and 1.3.
var duration = CGFloat(arc4random() % 0.8) / 1.3
or
var duration = CGFloat(arc4random() % 0.5) + 0.8
Thanks!
Here's a generic function I just wrote up quickly to get a random number within a range.
func randomBetween(_ firstNum: CGFloat, _ secondNum: CGFloat) -> CGFloat{
return CGFloat(arc4random()) / CGFloat(UINT32_MAX) * abs(firstNum - secondNum) + min(firstNum, secondNum)
}
It takes a random number, finds the remainder of that number divided by the difference between the two parameters, then adds by the smaller number. This guarantees the random number to be between the two numbers.
Disclaimer: I have not tested this out yet.
EDIT: Now this function does what you want.
Swift 5:
Using random(in:) which returns a random value within the specified range:
var duration = CGFloat.random(in: 0.8 ... 1.3)
Per Apple:
The random() static method chooses a random value from a continuous
uniform distribution in range, and then converts that value to the
nearest representable value in this type.
See random(in: using: ) to specify a random generator other than the default.
Related
What is the correct way to perform this operation?
399.9 / 100
What I would expect to see is
3.999
but the result is
3.9989999999999997
The result you see is correct, it's just not what you want.
Doubles are not precise values. The double you get by writing 399.9 is actually the precise value.
399.8999999999999772626324556767940521240234375
That's the closest available double to the exact value 399.9. Any other double is at least as far away from 399.9 as that.
Then you divide by 100. Again, the result is not precise, but the closest double has the exact value
3.99899999999999966604491419275291264057159423828125
That differs from what you would get by writing 3.999, which is the exact value:
3.999000000000000110134124042815528810024261474609375
At every step, the double operations have minimized the error, but because you are doing multiple steps, the final result diverges from the double closest to the mathematical result.
What you need to do depends on what your actual requirements are.
If you want to always calculate with two significant digits, then I'd just multiply my numbers with 100 and do all the operations as integer operations, until the very last division by 100.
If you have an intermediate result and wants to round it to two digits, I'd do what Fy Z1K says:
result = (result * 100).round() / 100;
import 'dart:math';
double roundDouble(double value, int places){
double mod = pow(10.0, places);
return ((value * mod).round().toDouble() / mod);
}
then you would basically get
double num1 = roundDouble(12.3412, 2);
// 12.34
double num2 = roundDouble(12.5668, 2);
// 12.57
double num3 = roundDouble(-12.3412, 2);
// -12.34
double num4 = roundDouble(-12.3456, 2);
// -12.35
To make decimal operations you can use the decimal package.
final d = Decimal.parse;
print(d('399.9') / d('100')); // => 3.999
just found this demo code to using parametric equation draw in iOS
you can see that key code is to generate this array of points
let points: [CGPoint] = 0.stride(to: M_PI * 2, by: 0.01).map
{
let x = pow(sin($0), 3)
var y = 13 * cos($0)
y -= 5 * cos(2 * $0)
y -= 2 * cos(3 * $0)
y -= cos(4 * $0)
y /= 16
return CGPoint(x: 320 + (x * 300), y: 280 + (y * -300))
}
which is just using this equation
now what I want to draw is more complex one mao curve
but the problem I encounter is there is a mathematic sign in the parametric equation provided by the site that I don't know how to convert to iOS code, this one
UPDATE: now I encounter a new problem: "Expression was too complex to be solved in reasonable time", any idea besides break it down into small expression like the heart curve did in its y above, because manually break it down it's too overwhelming
and the whole code is list below
func sgn(t: Double) -> Double{
switch t {
case _ where t < 0:
return -1.0
case _ where t > 0:
return 1.0
default:
return 0.0
}
}
func theta(t: Double) -> Double {
switch t {
case _ where t < 0:
return 0.0
case _ where t > 0:
return 1.0
default:
return 0.5
}
}
let pi = M_PI
let layer = CAShapeLayer()
layer.lineCap = kCALineCapRound
layer.lineJoin = kCALineCapRound
self.view.layer.addSublayer(layer)
let points: [CGPoint] = 0.stride(to: M_PI * 2, by: 0.01).map
{ t in
var x = ((-10/47*sin(30/41-17*t)-11/4*sin(83/58-15*t)-47/17*sin(79/53-13*t)+21080/33*sin(t+63/40)+179/32*sin(2*t+28/17)+263/25*sin(3*t+85/54)+1552/47*sin(4*t+146/31)+2015/77*sin(5*t+30/19)+286/31*sin(6*t+117/25)+1308/17*sin(7*t+67/42)+158/37*sin(8*t+77/17)+1017/26*sin(9*t+61/38)+107/18*sin(10*t+79/46)+863/39*sin(11*t+29/18)+74/23*sin(12*t+12/7)+110/63*sin(14*t+163/35)+29/31*sin(16*t+75/41)+2/7*sin(18*t+64/25)+305/37*sin(19*t+18/11)+9/34*sin(20*t+91/32)+316/45*sin(21*t+64/39)+16/15*sin(22*t+119/27)+179/35*sin(23*t+23/14)+38/51*sin(24*t+131/65)+1969/19)*theta(103*pi-t)*theta(t-99*pi)+(-44/31*sin(81/52-6*t)-47/19*sin(14/9-5*t)-206/75*sin(39/25-4*t)-77/16*sin(69/44-3*t)-416/25*sin(25/16-2*t)-1496/19*sin(47/30-t)-3315/19)*theta(99*pi-t)*theta(t-95*pi)+(732/7*sin(t+41/26)+111/17*sin(2*t+18/11)+187/20*sin(3*t+43/27)+37/24*sin(4*t+44/27)+157/45*sin(5*t+59/37)+5507/30)*theta(95*pi-t)*theta(t-91*pi)+(-19/42*sin(37/24-2*t)+3067/18*sin(t+11/7)+281/16*sin(3*t+96/61)+13/27*sin(4*t+159/34)+215/38*sin(5*t+30/19)+1121/34)*theta(91*pi-t)*theta(t-87*pi)+(-1/20*sin(17/14-16*t)+3995/36*sin(t+11/7)+353/25*sin(2*t+69/44)+209/18*sin(3*t+11/7)+349/66*sin(4*t+49/31)+67/17*sin(5*t+43/27)+84/43*sin(6*t+60/37)+79/30*sin(7*t+149/93)+93/38*sin(8*t+30/19)+37/22*sin(9*t+74/47)+91/57*sin(10*t+68/43)+43/29*sin(11*t+43/27)+42/23*sin(12*t+45/28)+28/31*sin(13*t+77/47)+17/36*sin(14*t+41/25)+17/86*sin(15*t+8/5)+12/29*sin(17*t+65/42)+11/31*sin(18*t+49/31)+31/47*sin(19*t+45/28)+13/31*sin(20*t+33/20)+5/43*sin(21*t+168/37)+9/46*sin(22*t+109/68)+26/29*sin(23*t+41/26)+31/47*sin(24*t+43/27)+5/21*sin(25*t+50/31)+2855/14)*theta(87*pi-t)*theta(t-83*pi)+(-5/33*sin(32/21-24*t)-16/17*sin(20/13-23*t)-64/39*sin(80/51-17*t)-1/28*sin(14/9-15*t)+3240/31*sin(t+11/7)+341/25*sin(2*t+52/33)+888/71*sin(3*t+63/40)+286/191*sin(4*t+31/19)+170/29*sin(5*t+68/43)+18/29*sin(6*t+80/17)+67/23*sin(7*t+91/58)+2/33*sin(8*t+36/31)+19/36*sin(9*t+51/32)+23/28*sin(10*t+21/13)+39/34*sin(11*t+65/41)+3/26*sin(12*t+39/20)+47/43*sin(13*t+37/23)+1/117*sin(14*t+825/206)+58/35*sin(16*t+47/30)+34/19*sin(18*t+62/39)+27/29*sin(19*t+127/27)+3/28*sin(20*t+245/52)+54/37*sin(21*t+53/33)+23/31*sin(22*t+21/13)-6998/27)*theta(83*pi-t)*theta(t-79*pi)+(2069/16*sin(t+11/7)+82/21*sin(2*t+14/3)+443/29*sin(3*t+19/12)+79/30*sin(4*t+30/19)+148/29*sin(5*t+19/12)+27/25*sin(6*t+33/20)+73/31*sin(7*t+27/17)+8893/37)*theta(79*pi-t)*theta(t-75*pi)+(-11/32*sin(31/22-2*t)+4451/49*sin(t+11/7)+515/68*sin(3*t+102/65)+1/20*sin(4*t+9/13)+79/30*sin(5*t+47/30)+17/37*sin(6*t+11/7)+47/30*sin(7*t+107/68)-10305/46)*theta(75*pi-t)*theta(t-71*pi)+(-5/24*sin(74/59-26*t)-22/25*sin(16/11-23*t)-5/29*sin(29/25-20*t)-62/41*sin(46/31-17*t)-48/25*sin(20/13-15*t)-113/24*sin(38/25-12*t)-231/40*sin(39/25-9*t)-89/39*sin(47/32-7*t)-104/19*sin(48/31-5*t)-182/15*sin(37/24-4*t)-140/19*sin(31/20-2*t)+3287/23*sin(t+107/68)+564/23*sin(3*t+113/24)+8/3*sin(6*t+13/8)+241/30*sin(8*t+37/23)+71/30*sin(10*t+14/9)+17/12*sin(11*t+44/27)+16/25*sin(13*t+67/39)+10/11*sin(14*t+81/52)+3*sin(16*t+21/13)+17/14*sin(18*t+127/27)+56/39*sin(19*t+117/73)+41/44*sin(21*t+159/34)+54/65*sin(22*t+27/17)+17/35*sin(24*t+159/34)+5/27*sin(25*t+44/29)-17/10)*theta(71*pi-t)*theta(t-67*pi)+(1987/31*sin(t+11/7)+305/28*sin(2*t+80/17)+751/28*sin(3*t+11/7)+191/30*sin(4*t+30/19)+313/33*sin(5*t+63/40)+122/73*sin(6*t+127/27)+113/34*sin(7*t+41/26)+23/30*sin(8*t+11/7)+73/27*sin(9*t+68/43)+3943/17)*theta(67*pi-t)*theta(t-63*pi)+(1593/29*sin(t+179/38)+191/29*sin(2*t+127/27)+678/97*sin(3*t+113/24)+44/19*sin(4*t+169/36)+63/34*sin(5*t+108/23)+17/16*sin(6*t+75/16)-6319/33)*theta(63*pi-t)*theta(t-59*pi)+(-9/29*sin(32/51-88*t)-39/29*sin(64/41-85*t)-19/27*sin(114/115-26*t)-30/43*sin(45/31-21*t)-14/11*sin(42/29-15*t)-13/38*sin(20/33-9*t)+15919/28*sin(t+39/25)+5859/34*sin(2*t+61/13)+1289/20*sin(3*t+229/49)+899/26*sin(4*t+26/17)+315/16*sin(5*t+145/31)+707/104*sin(6*t+26/17)+71/20*sin(7*t+3/2)+142/17*sin(8*t+188/41)+182/31*sin(10*t+28/19)+69/43*sin(11*t+356/79)+84/19*sin(12*t+55/39)+376/51*sin(13*t+87/62)+89/13*sin(14*t+67/46)+152/27*sin(16*t+69/47)+73/20*sin(17*t+57/43)+13/9*sin(18*t+3/2)+5/34*sin(19*t+70/41)+19/9*sin(20*t+47/36)+143/43*sin(22*t+43/33)+191/30*sin(23*t+31/24)+107/23*sin(24*t+35/27)+78/25*sin(25*t+32/25)+139/33*sin(27*t+43/33)+155/37*sin(28*t+43/33)+375/47*sin(29*t+141/106)+16/25*sin(30*t+145/38)+265/18*sin(31*t+232/53)+849/29*sin(32*t+61/14)+451/18*sin(33*t+204/47)+18/13*sin(34*t+327/77)+453/19*sin(35*t+6/5)+821/39*sin(36*t+43/37)+371/48*sin(37*t+37/32)+139/29*sin(38*t+107/25)+67/12*sin(39*t+343/78)+1033/100*sin(40*t+38/33)+435/59*sin(41*t+46/45)+327/43*sin(42*t+37/32)+264/53*sin(43*t+55/13)+19/35*sin(44*t+51/77)+771/71*sin(45*t+35/32)+85/24*sin(46*t+11/12)+30/23*sin(47*t+67/40)+139/22*sin(48*t+109/26)+170/41*sin(49*t+46/41)+195/32*sin(50*t+46/51)+154/71*sin(51*t+41/34)+63/17*sin(52*t+54/13)+42/25*sin(53*t+91/24)+185/77*sin(54*t+17/30)+773/103*sin(55*t+16/15)+68/11*sin(56*t+73/17)+113/24*sin(57*t+48/35)+351/53*sin(58*t+92/21)+643/60*sin(59*t+65/59)+153/23*sin(60*t+193/45)+62/25*sin(61*t+66/47)+37/33*sin(62*t+31/8)+79/20*sin(63*t+153/35)+213/25*sin(64*t+25/23)+721/68*sin(65*t+59/14)+237/28*sin(66*t+33/29)+20/7*sin(67*t+131/28)+242/41*sin(68*t+31/32)+57/86*sin(69*t+1/21)+86/21*sin(70*t+147/38)+203/102*sin(71*t+12/17)+159/47*sin(72*t+29/35)+23/16*sin(73*t+400/87)+28/25*sin(74*t+87/34)+377/113*sin(75*t+177/43)+173/61*sin(76*t+29/31)+25/21*sin(77*t+7/17)+69/19*sin(78*t+94/25)+34/33*sin(79*t+19/5)+9/28*sin(80*t+199/83)+140/93*sin(81*t+178/39)+339/127*sin(82*t+61/48)+107/20*sin(83*t+125/31)+81/20*sin(84*t+29/32)+27/26*sin(86*t+47/14)+9/25*sin(87*t+15/19)+149/75*sin(89*t+45/89)+29/21*sin(90*t+151/44)+75/26*sin(91*t+251/66)+50/21*sin(92*t+23/32)+62/55*sin(93*t+8/63)+23/11*sin(94*t+55/16)+51/26*sin(95*t+257/72)+38/25*sin(96*t+227/57)+314/209*sin(97*t+15/37)+11/9*sin(98*t+416/119)+11584/59)*theta(59*pi-t)*theta(t-55*pi)+(-43/31*sin(53/34-18*t)-58/35*sin(61/39-17*t)-55/19*sin(36/23-16*t)-155/59*sin(61/39-15*t)-45/19*sin(41/27-9*t)-46/13*sin(95/61-8*t)-360/23*sin(58/37-7*t)-77/15*sin(53/34-4*t)+27845/41*sin(t+91/58)+259/26*sin(2*t+212/45)+7843/86*sin(3*t+146/31)+2557/50*sin(5*t+146/31)+3931/786*sin(6*t+271/58)+31/23*sin(10*t+174/37)+79/24*sin(11*t+75/16)+86/47*sin(12*t+135/29)+97/24*sin(13*t+174/37)+76/29*sin(14*t+136/29)+30867/253)*theta(55*pi-t)*theta(t-51*pi)+(-5/31*sin(1/3-12*t)-23/52*sin(119/99-11*t)-166/33*sin(17/12-4*t)-196/41*sin(123/88-3*t)+327/34*sin(t+155/58)+297/40*sin(2*t+35/17)+35/44*sin(5*t+142/61)+3/5*sin(6*t+5/19)+11/19*sin(7*t+19/25)+23/58*sin(8*t+67/31)+12/31*sin(9*t+62/45)+5/12*sin(10*t+57/32)+27/13)*theta(51*pi-t)*theta(t-47*pi)+(2957/51*sin(t+7/22)+296/41)*theta(47*pi-t)*theta(t-43*pi)+(-1/2*sin(46/37-12*t)-233/35*sin(58/41-6*t)-491/47*sin(12/13-4*t)-227/12*sin(26/29-2*t)+7589/21*sin(t+23/18)+825/43*sin(3*t+19/24)+103/39*sin(5*t+7/12)+29/37*sin(7*t+43/14)+69/20*sin(8*t+130/31)+17/14*sin(9*t+137/56)+16/25*sin(10*t+129/37)+28/41*sin(11*t+71/29)+18979/130)*theta(43*pi-t)*theta(t-39*pi)+(-13/48*sin(31/24-7*t)-7/24*sin(29/25-6*t)-35/18*sin(59/69-2*t)+127/34*sin(t+7/55)+107/42*sin(3*t+94/21)+319/62*sin(4*t+569/122)+59/28*sin(5*t+22/13)+1/16*sin(8*t+139/37)+6/23*sin(9*t+4/27)+5/19*sin(10*t+119/41)+3/25*sin(11*t+44/19)+3/17*sin(12*t+338/89)-1419/7)*theta(39*pi-t)*theta(t-35*pi)+(-15/104*sin(31/38-10*t)-8/27*sin(26/29-8*t)-66/35*sin(4/23-4*t)-102/47*sin(41/36-t)+133/46*sin(2*t+31/13)+219/28*sin(3*t+93/35)+31/28*sin(5*t+182/67)+20/23*sin(6*t+5/38)+39/77*sin(7*t+119/53)+6/17*sin(9*t+95/28)+3/11*sin(11*t+73/24)+1/15*sin(12*t+26/29)+9908/49)*theta(35*pi-t)*theta(t-31*pi)+(-17/36*sin(29/32-3*t)-73/122*sin(53/38-2*t)+537/20*sin(t+5/4)+7/32*sin(4*t+184/41)+13/31*sin(5*t+21/5)+3/43*sin(6*t+9/22)+4507/22)*theta(31*pi-t)*theta(t-27*pi)+(-15/32*sin(40/31-6*t)-2/9*sin(2/7-5*t)-14/17*sin(73/55-4*t)-43/28*sin(50/37-2*t)+727/27*sin(t+20/17)+17/15*sin(3*t+4/35)-1416/7)*theta(27*pi-t)*theta(t-23*pi)+(-17/39*sin(35/33-16*t)-15/13*sin(5/8-14*t)-115/67*sin(52/35-10*t)-115/37*sin(11/45-7*t)-136/43*sin(26/25-6*t)-703/43*sin(41/28-4*t)-506/35*sin(1/114-3*t)-1405/34*sin(34/29-2*t)+413/48*sin(t+149/39)+241/32*sin(5*t+55/46)+195/44*sin(8*t+298/85)+73/27*sin(9*t+79/33)+59/28*sin(11*t+132/35)+13/22*sin(12*t+202/47)+11/20*sin(13*t+55/21)+99/74*sin(15*t+125/28)+5740/9)*theta(23*pi-t)*theta(t-19*pi)+(-23/26*sin(11/54-8*t)-112/29*sin(9/23-5*t)+137/33*sin(t+68/45)+33/19*sin(2*t+234/55)+604/57*sin(3*t+325/324)+541/47*sin(4*t+47/27)+36/19*sin(6*t+143/35)+37/28*sin(7*t+7/4)+26/33*sin(9*t+57/20)+24/49*sin(10*t+15/32)+1/8*sin(11*t+69/16)+17/42*sin(12*t+21/22)+901/40)*theta(19*pi-t)*theta(t-15*pi)+(4831/29*sin(t+46/29)+100/53*sin(2*t+38/29)+471/28*sin(3*t+49/29)+71/31*sin(4*t+95/31)+212/33*sin(5*t+38/25)+35/39*sin(6*t+164/55)+1163/291*sin(7*t+65/38)+14/23*sin(8*t+73/18)+467/36)*theta(15*pi-t)*theta(t-11*pi)+(-1/2*sin(4/25-8*t)-17/23*sin(1/20-6*t)-46/29*sin(1/12-4*t)-29/15*sin(36/37-2*t)+3308/37*sin(t+28/19)+576/67*sin(3*t+46/37)+87/25*sin(5*t+23/21)+78/47*sin(7*t+22/21)+3643/17)*theta(11*pi-t)*theta(t-7*pi)+(-41/20*sin(12/11-5*t)+9878/119*sin(t+76/33)+71/22*sin(2*t+133/29)+267/31*sin(3*t+76/21)+55/32*sin(4*t+3/31)+89/88*sin(6*t+23/24)+40/53*sin(7*t+3/25)+44/87*sin(8*t+34/11)+6/13*sin(9*t+40/27)-7131/31)*theta(7*pi-t)*theta(t-3*pi)+(-24/31*sin(16/17-16*t)-149/41*sin(50/37-9*t)-133/52*sin(23/32-8*t)-303/32*sin(1/734-7*t)+10523/20*sin(t+32/23)+431/20*sin(2*t+31/10)+1144/27*sin(3*t+139/36)+456/47*sin(4*t+81/34)+390/47*sin(5*t+146/81)+158/33*sin(6*t+14/27)+41/35*sin(10*t+43/20)+29/19*sin(11*t+82/33)+64/41*sin(12*t+42/31)+67/43*sin(13*t+47/43)+16/35*sin(14*t+41/20)+49/73*sin(15*t+59/88)+2388/37)*theta(3*pi-t)*theta(t+pi))*theta(sqrt(sgn(sin(t/2))))
var y = ((-407/57*sin(35/24-24*t)-42/31*sin(41/28-23*t)-49/16*sin(67/46-22*t)-26/37*sin(38/31-21*t)-76/35*sin(59/39-20*t)-310/73*sin(43/28-18*t)-11*sin(44/29-13*t)-580/41*sin(32/21-10*t)-3258/65*sin(63/41-9*t)-252/17*sin(56/37-8*t)-6254/51*sin(17/11-6*t)-1481/27*sin(71/46-5*t)-465/32*sin(73/47-3*t)+3651/50*sin(t+30/19)+8318/27*sin(2*t+30/19)+134/21*sin(4*t+127/27)+807/19*sin(7*t+19/12)+252/11*sin(11*t+155/97)+139/32*sin(12*t+49/30)+417/38*sin(14*t+51/31)+149/38*sin(15*t+117/70)+173/24*sin(16*t+13/8)+16/7*sin(17*t+12/7)+62/43*sin(19*t+87/56)-18494/25)*theta(103*pi-t)*theta(t-99*pi)+(-80/29*sin(47/30-5*t)-177/25*sin(113/72-3*t)-1901/33*sin(47/30-t)+599/31*sin(2*t+52/33)+77/41*sin(4*t+14/9)+11/9*sin(6*t+61/39)+2609/12)*theta(99*pi-t)*theta(t-95*pi)+(-1566/241*sin(58/37-3*t)-1857/50*sin(47/30-t)+464/31*sin(2*t+11/7)+59/24*sin(4*t+11/7)+20/17*sin(5*t+136/29)+7997/31)*theta(95*pi-t)*theta(t-91*pi)+(-15/29*sin(67/43-5*t)-129/25*sin(36/23-4*t)-148/61*sin(36/23-3*t)-103/33*sin(69/44-2*t)-29/17*sin(80/51-t)-4753/32)*theta(91*pi-t)*theta(t-87*pi)+(-24/19*sin(32/21-25*t)-229/98*sin(48/31-22*t)-83/31*sin(54/35-19*t)-37/9*sin(31/20-17*t)-46/19*sin(36/23-14*t)-71/23*sin(25/16-12*t)-29/19*sin(57/37-10*t)-49/15*sin(25/16-7*t)-396/113*sin(14/9-5*t)-181/36*sin(36/23-4*t)-176/25*sin(47/30-3*t)-1055/57*sin(80/51-2*t)-353/21*sin(113/72-t)+17/43*sin(6*t+63/40)+1/36*sin(8*t+1/22)+9/34*sin(9*t+50/33)+113/38*sin(11*t+47/30)+5/8*sin(13*t+27/17)+45/38*sin(15*t+174/37)+11/8*sin(16*t+61/39)+188/31*sin(18*t+19/12)+16/27*sin(20*t+61/13)+5/17*sin(21*t+23/13)+39/22*sin(23*t+68/43)+12/13*sin(24*t+25/16)+12966/29)*theta(87*pi-t)*theta(t-83*pi)+(-101/25*sin(23/15-23*t)-223/46*sin(25/16-19*t)-603/116*sin(80/51-17*t)-45/58*sin(14/9-15*t)-77/41*sin(65/42-11*t)-1/36*sin(59/98-10*t)-108/25*sin(47/30-9*t)-110/41*sin(39/25-7*t)-2479/826*sin(14/9-6*t)-166/33*sin(69/44-4*t)-266/23*sin(69/44-t)+62/33*sin(2*t+146/31)+1/7*sin(3*t+38/27)+1/64*sin(5*t+6/35)+27/23*sin(8*t+37/24)+49/40*sin(12*t+179/38)+39/11*sin(13*t+65/41)+53/71*sin(14*t+80/17)+255/98*sin(16*t+36/23)+110/17*sin(18*t+49/31)+8/33*sin(20*t+31/22)+38/11*sin(21*t+50/31)+35/32*sin(22*t+47/29)+11/23*sin(24*t+69/44)+52347/113)*theta(83*pi-t)*theta(t-79*pi)+(-128/59*sin(36/23-6*t)-75/29*sin(36/23-4*t)-853/40*sin(47/30-2*t)-1601/43*sin(102/65-t)+139/25*sin(3*t+39/25)+99/38*sin(5*t+19/12)+32/19*sin(7*t+81/52)+10547/30)*theta(79*pi-t)*theta(t-75*pi)+(-1/19*sin(57/37-7*t)+129/38*sin(t+41/26)+952/47*sin(2*t+212/45)+23/12*sin(3*t+169/36)+77/19*sin(4*t+146/31)+27/40*sin(5*t+35/22)+61/36*sin(6*t+146/31)+23311/66)*theta(75*pi-t)*theta(t-71*pi)+(-217/87*sin(49/32-8*t)+4673/114*sin(t+52/33)+1582/25*sin(2*t+30/19)+271/26*sin(3*t+65/41)+1036/29*sin(4*t+65/41)+151/43*sin(5*t+221/47)+240/19*sin(6*t+30/19)+277/31*sin(7*t+179/38)+346/35*sin(9*t+77/48)+136/23*sin(10*t+52/33)+41/13*sin(11*t+75/16)+29/23*sin(12*t+64/39)+23/35*sin(13*t+29/21)+5/27*sin(14*t+79/23)+12/31*sin(15*t+59/38)+41/16*sin(16*t+50/31)+17/30*sin(17*t+52/29)+5/17*sin(18*t+67/43)+23/25*sin(19*t+127/27)+43/38*sin(20*t+29/18)+11/17*sin(21*t+96/59)+23/45*sin(22*t+50/31)+13/44*sin(23*t+49/11)+20/33*sin(24*t+56/37)+1/12*sin(25*t+127/33)+4/27*sin(26*t+34/19)+3826/57)*theta(71*pi-t)*theta(t-67*pi)+(-67/36*sin(47/30-9*t)-629/118*sin(53/34-5*t)-49/30*sin(58/39-2*t)+7133/32*sin(t+245/52)+397/23*sin(3*t+146/31)+43/31*sin(4*t+48/31)+7/16*sin(6*t+50/33)+190/73*sin(7*t+245/52)+9/26*sin(8*t+18/11)-1822/15)*theta(67*pi-t)*theta(t-63*pi)+(4511/50*sin(t+179/38)+503/126*sin(2*t+65/41)+53/6*sin(3*t+179/38)+78/47*sin(4*t+51/32)+282/83*sin(5*t+80/17)+27/47*sin(6*t+51/32)+613/27)*theta(63*pi-t)*theta(t-59*pi)+(-8/9*sin(7/17-85*t)-12/17*sin(13/9-81*t)-22/31*sin(49/39-75*t)-29/41*sin(3/8-73*t)-22/19*sin(29/43-69*t)-31/47*sin(39/32-12*t)+353/3*sin(t+202/43)+4111/19*sin(2*t+61/13)+956/25*sin(3*t+14/9)+533/20*sin(4*t+89/19)+167/21*sin(5*t+183/40)+116/25*sin(6*t+131/28)+139/22*sin(7*t+55/36)+231/25*sin(8*t+149/32)+199/23*sin(9*t+70/47)+121/39*sin(10*t+649/139)+12/35*sin(11*t+66/35)+43/25*sin(13*t+17/11)+25/12*sin(14*t+79/17)+91/27*sin(15*t+41/29)+23/32*sin(16*t+205/44)+17/31*sin(17*t+121/27)+99/28*sin(18*t+114/25)+73/47*sin(19*t+19/13)+27/32*sin(20*t+193/43)+9/14*sin(21*t+64/33)+12/37*sin(22*t+197/46)+32/51*sin(23*t+119/29)+179/36*sin(24*t+190/43)+143/46*sin(25*t+131/30)+85/43*sin(26*t+146/33)+42/31*sin(27*t+536/119)+99/100*sin(28*t+108/23)+158/53*sin(29*t+76/65)+227/27*sin(30*t+27/23)+421/23*sin(31*t+17/14)+499/29*sin(32*t+65/54)+281/26*sin(33*t+73/61)+17/36*sin(34*t+78/17)+75/7*sin(35*t+178/41)+460/27*sin(36*t+125/29)+515/34*sin(37*t+73/17)+363/43*sin(38*t+179/42)+376/51*sin(39*t+412/97)+163/23*sin(40*t+38/9)+439/53*sin(41*t+174/41)+161/24*sin(42*t+127/30)+145/22*sin(43*t+106/25)+106/19*sin(44*t+135/32)+137/33*sin(45*t+67/16)+103/20*sin(46*t+245/57)+61/29*sin(47*t+94/23)+550/97*sin(48*t+171/40)+6/17*sin(49*t+187/72)+48/25*sin(50*t+102/23)+19/40*sin(51*t+41/12)+86/23*sin(52*t+101/24)+62/35*sin(53*t+157/40)+143/71*sin(54*t+251/57)+5/3*sin(55*t+41/29)+527/111*sin(56*t+47/11)+152/39*sin(57*t+47/39)+95/33*sin(58*t+49/11)+1381/345*sin(59*t+19/18)+167/125*sin(60*t+57/13)+19/31*sin(61*t+31/27)+37/32*sin(62*t+35/32)+63/94*sin(63*t+119/26)+16/13*sin(64*t+74/53)+29/6*sin(65*t+121/29)+57/47*sin(66*t+47/26)+61/27*sin(67*t+760/169)+241/39*sin(68*t+149/150)+31/12*sin(70*t+34/29)+49/37*sin(71*t+60/13)+73/37*sin(72*t+25/24)+25/28*sin(74*t+49/29)+42/23*sin(76*t+45/37)+69/25*sin(77*t+98/23)+41/27*sin(78*t+37/24)+29/15*sin(79*t+131/31)+16/11*sin(80*t+13/12)+29/28*sin(82*t+94/29)+11/21*sin(83*t+177/38)+32/17*sin(84*t+43/47)+31/28*sin(86*t+13/7)+145/42*sin(87*t+192/49)+49/32*sin(88*t+19/21)+67/48*sin(89*t+1/13)+33/28*sin(90*t+151/48)+40/21*sin(91*t+121/31)+33/20*sin(92*t+11/15)+106/49*sin(93*t+1/9)+17/19*sin(94*t+98/59)+7/3*sin(95*t+176/45)+97/29*sin(96*t+20/31)+50/27*sin(97*t+2/9)+17/7*sin(98*t+136/41)+16260/23)*theta(59*pi-t)*theta(t-55*pi)+(-142/41*sin(39/25-17*t)-68/33*sin(38/25-15*t)-25/12*sin(23/15-13*t)-23/12*sin(65/42-12*t)-497/33*sin(36/23-6*t)-105/19*sin(25/16-5*t)-1996/33*sin(47/30-4*t)-802/73*sin(113/72-t)+11015/34*sin(2*t+212/45)+354/35*sin(3*t+146/31)+109/14*sin(7*t+221/47)+16/15*sin(8*t+41/25)+217/69*sin(9*t+144/31)+17/13*sin(10*t+233/50)+82/29*sin(11*t+164/35)+3/7*sin(14*t+36/23)+224/79*sin(16*t+35/22)+43/14*sin(18*t+69/43)+7930/11)*theta(55*pi-t)*theta(t-51*pi)+(-13/25*sin(19/28-9*t)-16/33*sin(38/41-7*t)+181/15*sin(t+112/27)+159/32*sin(2*t+33/49)+259/38*sin(3*t+106/31)+57/13*sin(4*t+109/35)+8/27*sin(5*t+20/31)+35/22*sin(6*t+77/25)+41/32*sin(8*t+34/19)+23/42*sin(10*t+44/41)+22/35*sin(11*t+117/25)+17/32*sin(12*t+13/22)-9500/9)*theta(51*pi-t)*theta(t-47*pi)+(-2423/42*sin(36/29-t)-38027/36)*theta(47*pi-t)*theta(t-43*pi)+(-232/21*sin(59/47-5*t)-380/27*sin(2/17-4*t)-472/19*sin(19/18-3*t)+3307/21*sin(t+7/37)+1527/17*sin(2*t+76/65)+25/33*sin(6*t+23/20)+281/36*sin(7*t+134/33)+37/27*sin(8*t+57/25)+52/23*sin(9*t+54/13)+21/5*sin(10*t+19/9)+54/25*sin(11*t+172/51)+105/32*sin(12*t+53/46)-12945/26)*theta(43*pi-t)*theta(t-39*pi)+(-11/30*sin(36/43-11*t)-7/23*sin(3/11-7*t)-73/19*sin(1/49-3*t)-19/28*sin(4/31-t)+9/13*sin(2*t+21/62)+905/226*sin(4*t+4/25)+24/19*sin(5*t+111/40)+17/27*sin(6*t+41/23)+13/27*sin(8*t+57/20)+13/31*sin(9*t+5/27)+5/23*sin(10*t+59/18)+13/77*sin(12*t+123/37)+7606/23)*theta(39*pi-t)*theta(t-35*pi)+(-2/9*sin(7/25-11*t)-2/17*sin(17/32-9*t)-4/31*sin(25/17-7*t)-9/20*sin(41/39-5*t)+7/4*sin(t+53/28)+34/9*sin(2*t+109/26)+45/8*sin(3*t+217/51)+49/38*sin(4*t+37/36)+7/22*sin(6*t+90/181)+5/23*sin(8*t+11/25)+3/29*sin(10*t+169/41)+4/21*sin(12*t+305/76)+6727/20)*theta(35*pi-t)*theta(t-31*pi)+(-5/3*sin(23/34-3*t)-2989/130*sin(9/28-t)+13/30*sin(2*t+37/16)+5/6*sin(4*t+29/10)+15/41*sin(5*t+55/46)+5/23*sin(6*t+91/27)+9085/27)*theta(31*pi-t)*theta(t-27*pi)+(-56/45*sin(16/17-3*t)-2344/125*sin(19/43-t)+5/6*sin(2*t+20/19)+26/23*sin(4*t+75/31)+4/35*sin(5*t+654/187)+28/113*sin(6*t+118/37)+14511/44)*theta(27*pi-t)*theta(t-23*pi)+(-31/17*sin(29/48-14*t)-50/27*sin(33/98-13*t)-41/15*sin(11/39-10*t)-133/66*sin(5/18-8*t)-1171/19*sin(7/29-2*t)-1421/10*sin(27/22-t)+1347/59*sin(3*t+25/8)+942/43*sin(4*t+29/20)+740/49*sin(5*t+18/17)+47/8*sin(6*t+132/35)+167/13*sin(7*t+169/94)+67/19*sin(9*t+53/33)+16/81*sin(11*t+49/44)+35/18*sin(12*t+4/15)+9/23*sin(15*t+116/37)+19/11*sin(16*t+9/37)+2031/17)*theta(23*pi-t)*theta(t-19*pi)+(-23/30*sin(25/63-12*t)-163/109*sin(17/27-10*t)-sin(20/31-8*t)-104/11*sin(22/35-3*t)-98/27*sin(53/52-t)+138/55*sin(2*t+251/58)+177/17*sin(4*t+8/37)+269/84*sin(5*t+75/17)+28/29*sin(6*t+72/23)+20/19*sin(7*t+55/28)+68/45*sin(9*t+66/31)+31/37*sin(11*t+155/59)-6747/22)*theta(19*pi-t)*theta(t-15*pi)+(-103/38*sin(25/34-8*t)-439/219*sin(19/17-7*t)-159/31*sin(3/13-5*t)-35/26*sin(7/24-4*t)-387/83*sin(5/7-3*t)+944/19*sin(t+79/26)+329/31*sin(2*t+17/36)+25/12*sin(6*t+155/44)-4790/31)*theta(15*pi-t)*theta(t-11*pi)+(-84/31*sin(92/61-4*t)-3*sin(14/9-3*t)-381/31*sin(39/35-2*t)+925/46*sin(t+259/86)+48/43*sin(5*t+53/16)+27/29*sin(6*t+105/44)+11/21*sin(7*t+38/21)+7/31*sin(8*t+227/76)+11503/35)*theta(11*pi-t)*theta(t-7*pi)+(-16/21*sin(13/16-7*t)+245/12*sin(t+26/21)+199/20*sin(2*t+11/20)+47/26*sin(3*t+17/7)+70/51*sin(4*t+41/21)+22/9*sin(5*t+258/59)+29/31*sin(6*t+242/81)+7/24*sin(8*t+13/22)+26/37*sin(9*t+55/64)+8693/27)*theta(7*pi-t)*theta(t-3*pi)+(-16/9*sin(27/31-12*t)-103/38*sin(37/36-8*t)-63/25*sin(5/26-7*t)-58204/85*sin(4/21-t)+371/34*sin(2*t+109/27)+632/39*sin(3*t+71/27)+262/33*sin(4*t+13/16)+283/41*sin(5*t+43/24)+58/33*sin(6*t+79/32)+59/21*sin(9*t+77/20)+42/19*sin(10*t+81/35)+178/179*sin(11*t+17/19)+11/35*sin(13*t+67/16)+11/25*sin(14*t+14/13)+11/30*sin(15*t+135/29)+16/49*sin(16*t+70/43)+6851/32)*theta(3*pi-t)*theta(t+pi))*theta(sqrt(sgn(sin(t/2))))
return CGPoint(x: 320 + (x * 300), y: 280 + (y * -300))
}
let path = CGPathCreateMutable()
CGPathAddLines(path, nil, points, points.count)
layer.path = path
There a few issues here:
If look at the bottom of that formula on that web site, it defines θ to be the Heaviside step function.
You are doing integer division. You really want to be doing floating point division. E.g. rather than 10/47, you want 10.0/47.0.
You are striding from 0 to 2π, but you need to go from 0 to 104π.
These formulae are too complicated for Swift to compile. You'll want to split them up into separate lines.
In that equation you found online, there's something that looked curious, namely two occurrences of θ(sqrt(sgn(sin(t/2)))). Taking the square root of the sgn function is wrong (the is no real square root of -1). So I simplified those to just θ(sin(t/2)) and the curve behaved more reasonably.
Pulling this all together, I've taken your code sample, done some macros to split up those long formulae into separate statements, you end up with something like the code snippet here: https://gist.github.com/robertmryan/427f7e9b562ae153c7c1 (Stack Overflow is not letting me paste the full code here because it exceeds the maximum allowed length of a post.)
That yields the following image:
Note, all of those annoying lines back to the origin are a result of how this Mao curve is represented. It's not a single stroke, but rather a series of separate curves misleading stored as a single set of points. You could test for 0.0, 0.0, to eliminate that, but personally I'd break it up into separate arrays of points. I'll leave that for you.
I receive a float number from the user and check if it's valid depending on an increment defined (0.1, 0.5, 0.01, etc). For example, if the increment is 0.5, then 1.0, 1.5, 2.0 are valid but 1.2 is not. I'm using modulo to check if it is valid.
if (input % increment) == 0 {
println("Pass")
} else {
println("Fail")
}
The problem is that when the increment is 0.1, most of the valid values of input are detected as invalid.
I used the formula given in this answer and it improves the detection of valid inputs but still fails most of the time.
Your problem is that 0.1 isn't exactly representable in binary whereas 0.5 for instance is. this means that 0.1 * 5 doesn't equal 0.5. It's a lot like in decimal where (1 / 3) * 3 ≃ 0.3333333 * 3 = 0.99999999 not 1. To solve this sort of problem you can introduce an epsilon. An epsilon is a very small value, and you check whether your result is only a very small distance from the value you actually want, if it is you hope that the value is correct.
Based on James Snook's answer, I ended up with this solution.
let epsilon = 1e-6
let division = input / increment
let diff = abs(division - round(division))
if (diff < epsilon) {
println("Pass")
} else {
println("Fail")
}
I believe that the modulo works only with integers, if I'm right you should do your own workaround to see if the given number is a multiple or not, which translate to: the result of the division must be an integer.
The code should be something like this:
let floatDivision = input/increment;
let isInteger = floor(floatDivision) == floatDivision // true
if (isInteger) {
println("Pass")
} else {
println("Fail")
}
And this should work with any increment number (even more than one decimal point digit).
EDIT
as James Said, the float division of 1.2 over 0.1 is not coded exactly as 12 but 11.9999... So I added the epsilon in the comparison:
let input = 1.2;
let increment = 0.1;
let epsilon = 0.00000000000001;
let floatDivision = input/increment;
let dif = abs(round(floatDivision) - floatDivision);
println(String(format: "%.20f", floatDivision)); // 11.99999999999999822364
println(String(format: "%.20f", round(floatDivision))); // 12.00000000000000000000
println(String(format: "%.20f", dif)) // 0.00000000000000177636
let isInteger = dif < epsilon // Pass if (isInteger) {
println("Pass") } else {
println("Fail") }
Best of luck.
To handle numbers with one number after the decimal point you could do:
if (input * 10) % (increment * 10) == 0 {
println("Pass")
} else {
println("Fail")
}
To handle numbers with more than one number after the decimal point just increase the multiple. For example, if (input * 100000) % (increment * 100000) == 0.
I am trying to build a degrees/radians calculator. Here are the functions I have.
func degreesToRadians(degrees: Double) -> Double {
return degrees * (M_PI / 180)
}
func radiansToDegrees(radians: Double) -> Double {
return radians * (180 / M_PI)
}
degreesToRadians(90)
radiansToDegrees(M_PI/4)
degreesToRadians(90) returns 1.5707963267949
radiansToDegrees(M_PI/4) returns 45.0
What I want to happen is in the Degrees to Radians function instead of 1.5707963267949 I want the output to be π/2. Is this even possible?
Thanks.
To represent 90 degrees as π/2, what you want to do is
consider the fraction of degrees over 180 (i.e. numerator of degrees and denominator of 180);
reduce this fraction (i.e. divide both the numerator and denominator by the greatest common factor); in the case of 90/180, the greatest common factor is, in fact, 90, yielding a reduced numerator of 1 and a reduced denominator of 2;
the result as a string representation of the fraction, i.e. something like
"\(reducedNumerator)π/\(reducedDenominator)"
Obviously, if either the numerator or denominator are 1, then you can suppress that portion of the string, but hopefully you get the basic idea.
Take a crack at that.
If you want exactly: "π/2", I don't think this is possible with double. You might get this in String, but not in a number. I think your best option would be to take an optional bool in which case result is returned as multiple of pi.
degreesToRadians(90, resultAsMultipleOfPi:true)
If this bool is true then 0.5 should be returned. You may need to do some rounding off to get well rounded numbers.
If you look closely on what is really M_PI you will see that it is predefined double value in math.h that equals
#define M_PI 3.14159265358979323846264338327950288 /* pi */
If you want more precision you can declare and use long double value instead.
You can directly use M_PI constant.
If you need in float format just use this,
let pi = Float(M_PI)
I'm trying to generate a random number that's between 0 and 1. I keep reading about arc4random(), but there isn't any information about getting a float from it. How do I do this?
Random value in [0, 1[ (including 0, excluding 1):
double val = ((double)arc4random() / UINT32_MAX);
A bit more details here.
Actual range is [0, 0.999999999767169356], as upper bound is (double)0xFFFFFFFF / 0x100000000.
// Seed (only once)
srand48(time(0));
double x = drand48();
// Swift version
// Seed (only once)
srand48(Int(Date().timeIntervalSince1970))
let x = drand48()
The drand48() and erand48() functions return non-negative, double-precision, floating-point values, uniformly distributed over the interval [0.0 , 1.0].
For Swift 4.2+ see: https://stackoverflow.com/a/50733095/1033581
Below are recommendations for correct uniformity and optimal precision for ObjC and Swift 4.1.
32 bits precision (Optimal for Float)
Uniform random value in [0, 1] (including 0.0 and 1.0), up to 32 bits precision:
Obj-C:
float val = (float)arc4random() / UINT32_MAX;
Swift:
let val = Float(arc4random()) / Float(UInt32.max)
It's optimal for:
a Float (or Float32) which has a significand precision of 24 bits for its mantissa
48 bits precision (discouraged)
It's easy to achieve 48 bits precision with drand48 (which uses arc4random_buf under the hood). But note that drand48 has flaws because of the seed requirement and also for being suboptimal to randomize all 52 bits of Double mantissa.
Uniform random value in [0, 1], 48 bits precision:
Swift:
// seed (only needed once)
srand48(Int(Date.timeIntervalSinceReferenceDate))
// random Double value
let val = drand48()
64 bits precision (Optimal for Double and Float80)
Uniform random value in [0, 1] (including 0.0 and 1.0), up to 64 bits precision:
Swift, using two calls to arc4random:
let arc4random64 = UInt64(arc4random()) << 32 &+ UInt64(arc4random())
let val = Float80(arc4random64) / Float80(UInt64.max)
Swift, using one call to arc4random_buf:
var arc4random64: UInt64 = 0
arc4random_buf(&arc4random64, MemoryLayout.size(ofValue: arc4random64))
let val = Float80(arc4random64) / Float80(UInt64.max)
It's optimal for:
a Double (or Float64) which has a significand precision of 52 bits for its mantissa
a Float80 which has a significand precision of 64 bits for its mantissa
Notes
Comparisons with other methods
Answers where the range is excluding one of the bounds (0 or 1) likely suffer from a uniformity bias and should be avoided.
using arc4random(), best precision is 1 / 0xFFFFFFFF (UINT32_MAX)
using arc4random_uniform(), best precision is 1 / 0xFFFFFFFE (UINT32_MAX-1)
using rand() (secretly using arc4random), best precision is 1 / 0x7FFFFFFF (RAND_MAX)
using random() (secretly using arc4random), best precision is 1 / 0x7FFFFFFF (RAND_MAX)
It's mathematically impossible to achieve better than 32 bits precision with a single call to arc4random, arc4random_uniform, rand or random. So our above 32 bits and 64 bits solutions should be the best we can achieve.
This function works for negative float ranges as well:
float randomFloat(float Min, float Max){
return ((arc4random()%RAND_MAX)/(RAND_MAX*1.0))*(Max-Min)+Min;
}
Swift 4.2+
Swift 4.2 adds native support for a random value in a Range:
let x = Float.random(in: 0.0...1.0)
let y = Double.random(in: 0.0...1.0)
let z = Float80.random(in: 0.0...1.0)
Doc:
random(in range: ClosedRange<Float>)
random(in range: Range<Float>)
random(in range: ClosedRange<Double>)
random(in range: Range<Double>)
random(in range: ClosedRange<Float80>)
random(in range: Range<Float80>)
(float)rand() / RAND_MAX
The previous post stating "rand()" alone was incorrect.
This is the correct way to use rand().
This will create a number between 0 -> 1
BSD docs:
The rand() function computes a sequence of pseudo-random integers in the
range of 0 to RAND_MAX (as defined by the header file "stdlib.h").
This is extension for Float random number Swift 3.1
// MARK: Float Extension
public extension Float {
/// Returns a random floating point number between 0.0 and 1.0, inclusive.
public static var random: Float {
return Float(arc4random()) / Float(UInt32.max))
}
/// Random float between 0 and n-1.
///
/// - Parameter n: Interval max
/// - Returns: Returns a random float point number between 0 and n max
public static func random(min: Float, max: Float) -> Float {
return Float.random * (max - min) + min
}
}
Swift 4.2
Swift 4.2 has included a native and fairly full-featured random number API in the standard library. (Swift Evolution proposal SE-0202)
let intBetween0to9 = Int.random(in: 0...9)
let doubleBetween0to1 = Double.random(in: 0...1)
All number types have the static random(in:) function which takes the range and returns the random number in the given range.
Use this to avoid problems with upper bound of arc4random()
u_int32_t upper_bound = 1000000;
float r = arc4random_uniform(upper_bound)*1.0/upper_bound;
Note that it is applicable for MAC_10_7, IPHONE_4_3 and higher.
arc4random has a range up to 0x100000000 (4294967296)
This is another good option to generate random numbers between 0 to 1:
srand48(time(0)); // pseudo-random number initializer.
double r = drand48();
float x = arc4random() % 11 * 0.1;
That produces a random float bewteen 0 and 1.
More info here
rand()
by default produces a random number(float) between 0 and 1.