double - Using swift to calculate with very small values expressed in standard form -
i had long tedious homework calculate specific charge of first 20 elements' nuclei. being wishes not succumb typing in different numbers same formula on scientific calculator 20 times, tried code it. realised:
- swift hard use standard form natively
- small numbers seem cause problems
i took 2 inputs of a, , z being believe called nuclide number , proton number respectively , applied them formula:
using this, seemed cause problems value of e , mp, me, , mn small , couldn't seem work them. yet scientific calculator can , hand. how swift?
datasheet: http://filestore.aqa.org.uk/resources/physics/aqa-7408-sdb.pdf
edit:
my problem lied in fact not casting numbers correctly, believe caused them end being 0 in cases. question ended being rather pointless apologise for, i'll post solution cannot replicate original problem.
you need cast each value double retain accuracy. after value of sc calculated i've formatted display more see on calculator.
//homework: calculate specific charge of nucleus given it's nuclide notation import foundation let = 4 let z = 2 let e = 0 let sc = double(1.60*pow(10,-19)*double(z-e))/double( (double(z)*(1.673*pow(10,-27)))+(double(a-z)*(1.675*pow(10,-27)))+(double(e)*(1.99*pow(10,-31)))) let numberformatter = numberformatter() numberformatter.numberstyle = numberformatter.style.scientific numberformatter.positiveformat = "0.###e0" numberformatter.exponentsymbol = "x10^" if let stringfromnumber = numberformatter.string(from: sc nsnumber) { print(stringfromnumber) }
Comments
Post a Comment