Page 1 of 1

Maths - precision

Posted: Tue Jul 11, 2017 9:32 pm
by Simon Knight
I am attempting to convert a maths expression form LCS to LCB as my first attempt at doing more than "Hello World" with LCB. The complex part of original LC script is

Code: Select all

put (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2 into a
  put 2 * atan2( sqrt(a),sqrt(1-a)) into c
My LCB version is failing but it is correctly calculating a value for a.

LCB

Code: Select all

  put (sin(tChLat/2))^2 + cos(tLat1) * cos(tlat2) * (sin(tChLon/2))^2 into tFactorA
  log "tFactorA is"
  log tFactorA
  
   put 2 * atan2(((tFactorA)^(0.5)),((1-tFactorA)^(0.5))) into tFactorC

log "tFactorC is"
log tFactorC
The script version calculates a to be 7.61524e-05 whereas the LCB version logs 0.000076. It seems that LCB is not working to as many decimal places, is this the case or is it a function of the logging process.

While I am new to LCB I am left wondering why I might want to use it for general functions instead of a LC library script as it is the type of language environment that I was pleased to leave behind when I discovered Livecode and seems clumsy in comparison e.g. debugging the second complex line of maths is proving to be time consuming and may only serve to highlight a limitation in the precision of the maths functions.

Any thoughts?

best wishes

Simon

Re: Maths - precision

Posted: Wed Jul 12, 2017 4:21 am
by [-hh]
It's the numberformat of the log.

Code: Select all

put  10^6*2*atan2(0.0000761524^0.5,(1-0.0000761524)^0.5)
LCS result: 17453.290021

In LCB you have, compared to LCS, to interchange the arguments when using atan2:

Code: Select all

log  10^6*2*atan2((1-0.0000761524)^0.5,0.0000761524^0.5)
LCB result: 17453.290021

Re: Maths - precision

Posted: Wed Jul 12, 2017 6:48 am
by Simon Knight
Thanks for your reply, it is most helpful.

Simon