0 votes
by (180 points)

Hello,
I have a system which calculates a final value from the sum of several
different variables by adding the values of those variables together.

First, the variables must be multiplied by a percentage, in this case represented by a value between 0 and 1, with 0.00 being 0% and 1.00 being 100%:

<<set $VariableDifference1 to 0>>
<<set $MainValue to 100>>
<<set $PercentageValue to 0.056>>

$VariableDifference1
$MainValue
$PercentageValue

<<set $VariableDifference1 to $MainValue * $PercentageValue>>\

$VariableDifference1
$MainValue
$PercentageValue

And in this case, as you can see if you try it for yourselves,
$variableDifference1 becomes 5.6000000000000005 rather than 5.6. Due to
the nature of the system I am creating, answers regarding percentages etc
would need to be as precise and exact as possible. I am also unsure if
the actual addition side is correct or not, it only uses a fairly simple

<<set $x3 to $x1 + $x2>>

Type thing, but I know that the end result is often incorrect because to
multiply a number by a percentage I personally can't think of any way
other than multiplying it by a number with a decimal place, and I've had
this explained to me only recently by MadExile, but because of the way
the system performs calculation, it will sometimes return numbers with
multiple decimal places even if it's adding/multiplying two whole numbers
together.

To summarise, I'm in need of a method that could calculate the percentage
of a value as exactly as possible with as little margin for error as
possible. Thank you.

1 Answer

0 votes
by (159k points)

Your previous My Percentage Amalgamation system calculates certain number values incorrectly thread contains the information on how to handle the conversion of a decimal number both to and from an integer using scientific E-notation.

In this new example you are using a percentage significant to three decimal places, so you would need to change the conversion process to take that into consideration.

<<set $VariableDifference1 to 0>>
<<set $MainValue to 100>>

<<set $PercentageValue to Number(0.056 + 'e3')>>   /% eg. 56 %/

$VariableDifference1
$MainValue
$PercentageValue

<<set $VariableDifference1 to Number($MainValue * $PercentageValue + 'e-3')>>\

$VariableDifference1
$MainValue
$PercentageValue

... notice the change from being significant to two decimal places (eg. the e2 and e-2) to now being significant to three decimal places (eg. the e3 and e-3)

by (180 points)

Much Obliged greyelf; so far so good! I wasn't at all aware that the number next to the 'e' represented how many digits would be translated to whole numbers. Interesting. Even so, I'm still slightly concerned that if, for example, I add two separate numbers from these calculations with the standard macros;

(As of me writing this post, the code format button isn't currently in the toolbar in it's regular place, nor can I see it elsewhere for whatever reason.)

<<set $x3 to $x1 + $x2>>

The above could still have a chance of generating a resulting answer that could be mathematically incorrect. Is there any way to address this issue, or to place some kind of code that calculates sums as a calculator would rather than with the standard multiple floating points etc?

by (159k points)

<<set $x3 to $x1 + $x2>>

Could you supply an example where addition of decimal point numbers results in an incorrect value?

One of the points made in the other thread was to not use decimal point number unless absolutely necessary, and this principle is used through out software in general.

eg. In currency related software $8.75 would generally be 875 cents internally and only converted to a decimal number at the point it is displayed on screen.

I suggest you do the same with the numerical values within your story.

...