calculation problem

Just starting out? Need help? Post your questions and find answers here.
rvq4u
Posts: 9
Joined: Sat Sep 14, 2024 7:20 pm

calculation problem

Post 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.
hoerbie
Posts: 137
Joined: Sun Mar 17, 2019 5:51 pm
Location: DE/BY/MUC

Re: calculation problem

Post 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
rvq4u
Posts: 9
Joined: Sat Sep 14, 2024 7:20 pm

Re: calculation problem

Post 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
User avatar
Paul
Posts: 210
Joined: Wed Feb 26, 2014 6:46 pm
Location: Canada
Contact:

Re: calculation problem

Post by Paul »

As hoerbie said, use the proper function...

Code: Select all

Debug StrD(((3*60)+30)/60)
hoerbie
Posts: 137
Joined: Sun Mar 17, 2019 5:51 pm
Location: DE/BY/MUC

Re: calculation problem

Post 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
Post Reply