Page 1 of 1

calculation problem

Posted: Tue Sep 24, 2024 4:23 pm
by rvq4u
Hi the problem i found is in both languages to find (spiderbasic and Purebasic)

if i force
Purebasic or Spiderbasic

Debug Str(((3*60)+30)/60)

both return 3 and not 3,5 it seems something to go wrong.

Re: calculation problem

Posted: Tue Sep 24, 2024 5:02 pm
by hoerbie
What goes wrong, is that you are using the wrong function, please have a look at the "Remarks" section of the SB docs:
https://www.spiderbasic.com/documentati ... g/str.html

Re: calculation problem

Posted: Tue Sep 24, 2024 7:04 pm
by rvq4u
it returns

Result: 100000000000000000

it kills a 1 in se last line
and some changes

Value.q = 2.522
Debug "Result: " + Str(Value)

Result: 3

Value.q = 2.22
Debug "Result: " + Str(Value)
Result: 2

Re: calculation problem

Posted: Wed Sep 25, 2024 1:02 am
by Paul
As hoerbie said, use the proper function...

Code: Select all

Debug StrD(((3*60)+30)/60)

Re: calculation problem

Posted: Wed Sep 25, 2024 7:38 am
by hoerbie
Sorry, I don't understand what you mean by
Result: 100000000000000000
it kills a 1 in se last line
And your new examples like
Value.q = 2.522
Debug "Result: " + Str(Value)
can't work now because of two errors.

Paul directly posted the right function, that I thought you could find yourself from my last post:
https://www.spiderbasic.com/documentati ... /strd.html
So in clear words: to print out a decimal value you need to use the StrD() function.

But changing the function in your example will not do anything, because you are additionally using the wrong data type for your variable "Value". Please read and understand the section under "Variables declaration" and "Basic types":
https://www.spiderbasic.com/documentati ... ables.html
In clear words: your decimal value 2.522 is directly cut (or rounded) from SB or PB when you try to save it in a variable type like .q that only can handle non-decimal numbers. You need to use the .d type for decimal variables.

And as additional information: when you are now using the right type and function for your above example, you will find new problems, that SB or PB output a slightly different value than you put in the variable. This is also not a bug.
For this please read and understand the last section "Special information about Floats and Doubles" of the https://www.spiderbasic.com/documentati ... ables.html