Maths - precision

LiveCode Builder is a language for extending LiveCode's capabilities, creating new object types as Widgets, and libraries that access lower-level APIs in OSes, applications, and DLLs.

Moderators: LCMark, LCfraser

Post Reply
Simon Knight
Posts: 854
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Maths - precision

Post by Simon Knight » Tue Jul 11, 2017 9:32 pm

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
best wishes
Skids

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Maths - precision

Post by [-hh] » Wed Jul 12, 2017 4:21 am

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
shiftLock happens

Simon Knight
Posts: 854
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Maths - precision

Post by Simon Knight » Wed Jul 12, 2017 6:48 am

Thanks for your reply, it is most helpful.

Simon
best wishes
Skids

Post Reply

Return to “LiveCode Builder”