
adccommunitymod (AutomationDirect) asked a question.
MATH. Log vs Ln round or truncate
Created Date: February 10,2020
Created By: kewakl
**** This post has been imported from our legacy forum. Information in this post may be outdated and links contained in the post may no longer work.****
I am attempting to use Log() to find yhe magnitude of a value. I am using S32 tags as the value and the result. I have used Log() successfully in other languages, but it seems that PAC rounds. Can I coerce the math instruction to truncate the result or will I be forced to long-hand this operation? Ideas?
Created Date: February 10,2020
Created by: kewakl
I am attempting to use Log() to find yhe magnitude of a value.
I am using S32 tags as the value and the result.
I have used Log() successfully in other languages, but it seems that PAC rounds.
Can I coerce the math instruction to truncate the result or will I be forced to long-hand this operation?
Ideas?
Created Date: February 10,2020
Created by: ADC_CommTeam02
So I did both LOG & LN functions. I agree they both round. But if you multiply by 10 then integer divide by 10 it will strip off the remainder.
{ "alt ": "Click image for larger version Name:\tmath1.png Views:\t0 Size:\t62.3 KB ID:\t128373 ", "data-align ": "none ", "data-attachmentid ": "128373 ", "data-size ": "full "}
Since, at values (examples posted earlier), the INT_MULDIV and INT_DIV workaround dance around CORRECT and INCORRECT results,
I have a preliminary workaround to coerce the MATH log() function to deliver a 'correct' result.
The ' % 1 ' term will return the decimal part of the log() result, so
' - ( log( F32_1 ) % 1 ) ' will remove that decimal portion - no worrying about the trunc/round issue.
I have tested this with two values : one on either side of the issue.
F32_1 = 316.227 log( 316.227 ) = 2.4999989479820419262299059532605
F32_1 = 316.228 log( 316.228 ) = 2.5000003213429352818480616426855
MATH log( 316.227) returns 2 - CORRECT
MATH log( 316.228) returns 3 - INCORRECT
MATH log( 316.227) - ( log( 316.227) % 1) returns 2 - CORRECT
MATH log( 316.228) - ( log( 316.228) % 1) returns 2 - CORRECT
I accidentally arrived at this approach this morning while considering this post.
I think that more testing is in my future. (If anyone already suggested this, and I missed it, I apologize to you - and want to thank you!)
Nope. I cannot make this 100 percent accurate.
At 1000, log(1000.0) = 3, 3 MOD 1
=1=2.Thus:
Powers of 10 at intervals of 3 also fail in the same way.