How to round/convert a constant to integer?

Just starting out? Need help? Post your questions and find answers here.
User avatar
matalog
Enthusiast
Enthusiast
Posts: 305
Joined: Tue Sep 05, 2017 10:07 am

How to round/convert a constant to integer?

Post by matalog »

If I define a couple of constants, #scale=#width/1.9 that is fine and allowed, but if I definitely want that constant to be an integer, then I don't seem to be allowed to round the value or set it as a variable that was rounded. Int() doesn't seem to be allowed on constants either.

What options do I have?
User avatar
Demivec
Addict
Addict
Posts: 4281
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: How to round/convert a constant to integer?

Post by Demivec »

matalog wrote: Fri Jul 26, 2024 12:18 pm What options do I have?
My idea, untested, is to scale calculation up to use only integers and then scale it back down.

Code: Select all

scale=((#width * 100) / 190) / 100
ebs
Enthusiast
Enthusiast
Posts: 561
Joined: Fri Apr 25, 2003 11:08 pm

Re: How to round/convert a constant to integer?

Post by ebs »

I'm confused - the code below works fine. Is this what you're trying to do or did I misunderstand?

Code: Select all

#width = 800
#scale=#width/1.9

Debug #scale          ; prints "421.0526315789474"
Debug Int(#scale)     ; prints "421"
var = Int(#scale)
Debug var             ; prints "421"
User avatar
STARGÅTE
Addict
Addict
Posts: 2260
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: How to round/convert a constant to integer?

Post by STARGÅTE »

Code: Select all

#width = 800
#scale = #width * 10/19

Debug #width
Debug #scale
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
DarkDragon
Addict
Addict
Posts: 2347
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: How to round/convert a constant to integer?

Post by DarkDragon »

OP never restricted the data type of width to integer though.
bye,
Daniel
ebs
Enthusiast
Enthusiast
Posts: 561
Joined: Fri Apr 25, 2003 11:08 pm

Re: How to round/convert a constant to integer?

Post by ebs »

OP never restricted the data type of width to integer though.
Does it matter?

Code: Select all

#width = 832.9
#scale=#width/1.9

Debug #width          ; prints "832.89999999999998"
Debug #scale          ; prints "438.36842105263156"
Debug Int(#scale)     ; prints "438"
var = Int(#scale)
Debug var             ; prints "438"
DarkDragon
Addict
Addict
Posts: 2347
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: How to round/convert a constant to integer?

Post by DarkDragon »

ebs wrote: Fri Jul 26, 2024 3:41 pm
OP never restricted the data type of width to integer though.
Does it matter?
Yes, he wants to define the constant depending on another constant and the result should be integer. OP doesn't want to use Int(#scale) everywhere, it could be easily forgotten to call Int(), the constant itself is required to contain the integer value to make it less error prone.
bye,
Daniel
Post Reply