Minus from Table Item fails, Bug or feature?

Report any Hollywood bugs here
Post Reply
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Minus from Table Item fails, Bug or feature?

Post by Bugala »

I wasnt able to make an example program that would have triggered this problem, therefore I am copy-pasting three lines of my code, and showing the Debugmessages, which show that something is wrong, but what?

Code: Select all

If CurrencyLost = "xrp" Then DebugPrint("XRP Balance: "..CoinHoldingPlaces[Place][CurrencyLost].." Lost amount: "..LostAmount)
		CoinHoldingPlaces[Place][CurrencyLost] = CoinHoldingPlaces[Place][CurrencyLost] - LostAmount
If CurrencyLost = "xrp" Then DebugPrint("New Balance: "..CoinHoldingPlaces[Place][CurrencyLost])
Result, as an example:

Code: Select all

XRP Balance: 145 Lost amount: 145
New Balance: -2.7284841053188e-012
I am suspecting if it is possible that one of these is not a number, but something else, but what can it be?

As far as I know, everything is Numbers, I have even used ReplaceStr() to remove unnecessary characters and then used ToNumber() to make them into numbers. And in Addition I even used GetType() to see that they are numbers.

Also, It seems there is no problem when I am using "+"-operator, problem is only when I use "-" operator.

In Addition, to take out the possibility of having misspelled something, i also did following:

Code: Select all

Local num1 = CoinHoldingPlaces[Place][CurrencyLost]
				DebugPrint("number: "..#NUMBER)
				DebugPrint("num1: "..GetType(num1).." LostAmount: "..GetType(LostAmount))
				DebugPrint("num1: "..num1.." LostAmount:"..LostAmount)
Local num2 = num1 - LostAmount
				DebugPrint("num1 - LostAmount: "..num2)

And still result was:

Code: Select all

number: 2
num1: 2 LostAmount: 2
num1: 145 LostAmount:145
num1 - LostAmount: -2.7284841053188e-012
Is this a Bug or a feature? Is it possible one of the numbers is not actually a number, and how can I check that if GetType didnt reveal it?

edit: Took a try to change instead of using "-" using "Sub()", but that didnt work either, but result was still the same.

edit2: Tried to trick it with following idea:

Code: Select all

LostAmount2 = 0 - LostAmount
Add(num1, LostAmount2)
(also tried using "+" operator) but still no use, result is still the same, which is even more strange, since to show more of the code:

Code: Select all

If CurrencyGained <> "" 
			If HaveItem(CoinHoldingPlaces[Place], CurrencyGained) = False Then CoinHoldingPlaces[Place][CurrencyGained] = 0 
			CoinHoldingPlaces[Place][CurrencyGained] = CoinHoldingPlaces[Place][CurrencyGained] + GainedAmount
EndIf
If CurrencyLost <> "" 
			CoinHoldingPlaces[Place][CurrencyLost] = CoinHoldingPlaces[Place][CurrencyLost] - LostAmount

EndIf
Point being that when it does the above ones "+" operator, there is no problem at all. It only has problem when it is doing the "-" side, and yet these two should be identical as far as I know.

Edit3:
I changed

Code: Select all

Local num2 = num1 - LostAmount
into

Code: Select all

Local num2 = num1 + LostAmount
and it works fine as long as both numbers are on plus side. It seems to be only having problem when subtraction happens, whether with "-" operator, or due to other number being less than 0.
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Minus from Table Item fails, Bug or feature?

Post by Bugala »

Found out a bit more.

Problem is not with the "LostAmount" variable, problem is with the CoinHoldingPlaces[Place][CurrencyLost]

For I changed LostAmount into direct number, and bug still happened.

However, changing num1 into manually something, like num1 = 145, it works right.

In addition, I kept checking debugprints better, and noticed that the program seems to run fine for some time, it is only at that certain point, actually exactly the 145-145 which seems to trigger the problem, and after that, all subtractions keep failing.
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Minus from Table Item fails, Bug or feature?

Post by Bugala »

In case someone follows and is interested, I have sent Andreas a stripped-down code example that triggers the bug.
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Minus from Table Item fails, Bug or feature?

Post by Bugala »

was able to make minimum code amount to trigger the bug.

Hollywood 9.1 Windows 10.

Can someone else confirm they get the same result as me:

Code: Select all

CoinHoldingPlaces = { 
		["southexchange"] = { 
		["bigo"] = { plus = 300, neg = 0 },
		["sc"] = { plus = 145880 , neg = 145879.99999903 },
		["xbtx"] = { plus = 5001.72343, neg = 5001.1},
		["goss"] = { plus = 3000, neg = 0 },
		["telos"] = { plus = 17082.149576664, neg = 16502.740448774 },
		["btc"] = { plus = 0.078870939137457 , neg = 0.046210885491437 }
					},
			    }

Function PrintCurrentCoinBalances(BalancesDataBase)
	ForEach(BalancesDataBase, Function (CoinHoldingPlaceName, Stuff)
		DebugPrint("CoinHoldingPlace: "..CoinHoldingPlaceName)
		 ForEach(Stuff, Function(CoinTicker, Balance)
			Local sum = Balance.Plus - Balance.Neg
			DebugPrint(CoinTicker..": "..CoinTicker.. " > "..Sum.." (PLUS: "..Balance.plus.." - NEG: "..Balance.Neg..")")
				EndFunction)
				   EndFunction)
EndFunction


PrintCurrentCoinBalances(CoinHoldingPlaces)

result:

Code: Select all

CoinHoldingPlace: southexchange
bigo: bigo > 300 (PLUS: 300 - NEG: 0)
sc: sc > 9.7000156529248e-007 (PLUS: 145880 - NEG: 145879.99999903)
xbtx: xbtx > 0.62342999999964 (PLUS: 5001.72343 - NEG: 5001.1)
goss: goss > 3000 (PLUS: 3000 - NEG: 0)
telos: telos > 579.40912789 (PLUS: 17082.149576664 - NEG: 16502.740448774)
btc: btc > 0.03266005364602 (PLUS: 0.078870939137457 - NEG: 0.046210885491437)
Notice the result line:

Code: Select all

xbtx: xbtx > 0.62342999999964 (PLUS: 5001.72343 - NEG: 5001.1)
Idea being that first number: 0.62342999... is the result of num1 - num2, where num1 is PLUS and num2 is NEG, which makes the result impossible to be right as it adds extra digits that dont exist in original two numbers.
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Minus from Table Item fails, Bug or feature?

Post by Bugala »

Actually, just noticed that this specific number combination fails every time, so here is the new minimum code amount

Code: Select all

sum = 5001.72343 - 5001.1
DebugPrint("sum: "..sum)
results in:

Code: Select all

sum: 0.62342999999964
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Minus from Table Item fails, Bug or feature?

Post by Bugala »

I can't confirm this yet, but I am suspecting this bug might be reflecting elsewhere too.

It might be affecting one of Adding, Multiplying, or Dividing.

My guess is that it is not affecting Adding, since I have enough Adding calculations that do work as supposed.

In addition, I am also suspecting that this might affect adding entries to a Table in such a way that it makes two entries somehow.

For the thing is, in trying to circumvent the problem with subtraction, I made my program in such a way that it adds all the profits and all the losses together, and only at the end, it does exactly one subtraction.

As example:

profits = profit1 + profit2 + profit3
expenses = expense1 + expense2 + expense3

total = profits - expenses.


Now problem is that when I look at these expenses, I see there coming more expenses than there should, and as I was debugprinting individual entries, there came things like:

entry1: 0.000253525 (this is real and right)
entry2: 0.00025352536500042 (there is none like this, and it looks more like an erroneous calculation result from entry 1 that somehow became its own entry)

At this point, I can't confirm yet that this Entry 2 wouldn't be a result of a bug in my code, but I am suspecting this entry could be a result of this calculation error somehow, since I have difficulty figuring out how it could have become to exist any other way.
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Minus from Table Item fails, Bug or feature?

Post by airsoftsoftwair »

Bugala wrote: Mon May 16, 2022 1:10 pm Actually, just noticed that this specific number combination fails every time, so here is the new minimum code amount

Code: Select all

sum = 5001.72343 - 5001.1
DebugPrint("sum: "..sum)
results in:

Code: Select all

sum: 0.62342999999964
That's not a bug, it's the wonderful world of floating point arithmetic. You can't expect neither 5001.72343 nor 5001.1 nor the result to be stored as the exact numbers. Instead, they're often just approximations. Try the Lua demo here with your code, it'll show the very same result.
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Minus from Table Item fails, Bug or feature?

Post by Bugala »

So is there a way to get around it?

That how can I make that calculation and receive the right answer?
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Minus from Table Item fails, Bug or feature?

Post by airsoftsoftwair »

Bugala wrote: Fri May 20, 2022 2:05 pm So is there a way to get around it?
Well, you can always use strings and set the desired precision, e.g.

Code: Select all

DebugPrint(FormatStr("%.5g", 5001.72343 - 5001.1))
This will print the expected result.
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Minus from Table Item fails, Bug or feature?

Post by Bugala »

Okay, this is a partial fix, but still not completely working.

When using "%.5g" it does result in the right answer, and even when using "%.9g" (which is what I need to use) it still works fine, but using for example "%.13g", it happens to result in 0.6234299999996, since the 14th digit is 1, and rounded down, therefore, leaving all these 9s in place.
And even if it would round up, it would still just round the last 6 to 7, leaving the 9s still intact.

Problem is that without checking I won't know how many digits these numbers have, and for that reason would be using "%.9g".

When it comes to addition, subtraction, and multiplication, I could calculate the number of digits after the numbers and then use the correct number based on that.

However, when it comes to division, Divisions result might end up having any number of digits in the result. If there is no better solution for this, then I guess I have to make my own division function which will do the division number by number the school taught way to be aware of how many digits it ends up having (and to stop at 9th digit).

So is there any better solution to make sure that I get the correct answer for all the calculations?

Also, one more thing. Is it a sure thing that the extra digit will always be 9? or at least between 5 to 9, as in rounding up, since if the extra digit ends up being less than 5, then it would round down the last remaining digit, resulting in a wrong answer.

As in the example case, the right answer is 0.6234--3-- but if the first removed digit would be between 0 and 4, then the result would end up being 0.6234--2--, which would be wrong again.
Post Reply