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:

  1. swift hard use standard form natively
  2. small numbers seem cause problems

i took 2 inputs of a, , z being believe called nuclide number , proton number respectively , applied them formula:

specific charge 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

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -