Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Author
Message
KayDee
4
Years of Service
User Offline
Joined: 12th Feb 2020
Location:
Posted: 10th Apr 2020 16:08
Is there an equivalent Int() function in AGK.

I've tried the help and couldn't find it.
Bengismo
6
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 10th Apr 2020 16:35
chafari
Valued Member
17
Years of Service
User Offline
Joined: 2nd May 2006
Location: Canary Islands
Posted: 10th Apr 2020 17:04
I use trunc....
I'm not a grumpy grandpa
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 10th Apr 2020 19:58
the core functions are listed here https://www.appgamekit.com/documentation/Reference/Core.htm

fubarpk on Itch...………...https://fubarpk.itch.io/
fubarpk on googleplay..https://play.google.com/store/apps/developer?id=fubarpk
TomToad
6
Years of Service
User Offline
Joined: 6th Jan 2018
Location:
Posted: 10th Apr 2020 22:11
KayDee
4
Years of Service
User Offline
Joined: 12th Feb 2020
Location:
Posted: 10th Apr 2020 22:17
Thanks guys...like I mentioned before, I was spoilt by Blitz.



end of line
Raven
19
Years of Service
User Offline
Joined: 23rd Mar 2005
Location: Hertfordshire, England
Posted: 10th Apr 2020 22:35
It's not just Blitz... Dark BASIC and Dark BASIC Professional had Int( ) as well.
Why they changed it is anyone' guess.

You'll find as you use it more, you'll have a lot of moments where you're like "Why the F have they implemented it that why?!"... especially coming from DBP / Blitz / C++
SFSW
21
Years of Service
User Offline
Joined: 9th Oct 2002
Location:
Posted: 10th Apr 2020 22:51
On the other hand, you may find that although the syntax may be different in certain instances, the capabilities are quite flexible and extensive. You can also always drop to Tier2 if you want that formatting. But in the case of AGK/S directly, the core operations and operators listed at the link 'fubarpk' posted above provide a list of what you can do far beyond just rounding one or two ways. Also, the string operators are quite handy (and fast) compared to some languages that you would otherwise have to write your own functions for. Other operators are there also (ie ~~ XOR, bitshift, etc). It's well worth learning what's available.

Conjured Entertainment
AGK Developer
18
Years of Service
User Offline
Joined: 12th Sep 2005
Location: Nirvana
Posted: 11th Apr 2020 15:05
Quote: "It's not just Blitz... Dark BASIC and Dark BASIC Professional had Int( ) as well.
Why they changed it is anyone' guess.

You'll find as you use it more, you'll have a lot of moments where you're like "Why the F have they implemented it that why?!"... especially coming from DBP / Blitz / C++"


I had tried using int() as well out of habit from other languages.

Sometimes I still do, forgetting the val() as old habits are hard to break.

Coding things my way since 1981 -- Currently using AppGameKit V2 Tier 1
Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
SFSW
21
Years of Service
User Offline
Joined: 9th Oct 2002
Location:
Posted: 11th Apr 2020 19:26 Edited at: 11th Apr 2020 19:28
Scraggle wrote: "
b = a#
Job done"


Normally, that would work. However, AGK/S uses a more mathematical rounding rather than programmatic. So in your example, if a#=1.52, it would round to 2. If a#=1.48, it would round to 1. If your code needs traditional programmatic rounding (where 1.9 would still round to 1), then you need to use 'trunc(a#)' instead. So 'trunc()' is a direct replacement for 'int()' to round down to the nearest integer.

'val()' generally deals with string to integer and another detail to be aware of there is the distinction between integer and float. While in DBPro, you could use 'val()' to retrieve a numerical value for either an integer or a float, in AGK/S, you need to use 'val()' for integers and 'valfloat()' for floats.
Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Conjured Entertainment
AGK Developer
18
Years of Service
User Offline
Joined: 12th Sep 2005
Location: Nirvana
Posted: 11th Apr 2020 22:45 Edited at: 11th Apr 2020 23:26
I thought int() AKA val() was for converting strings to integers, not floats to integers

such as...

b = int(a$)
...or...
b = val(a$)
...in AGK

with the opposite being converting integers to strings with str() as in ...

a$=str(b)


and if you want to round a float to the nearest integer then use the round() command...

b = round(a#)

or round it up to the next integer with the ceil() command...

b = ceil(a#)

or convert the float to an integer without rounding with the trunc() command...

b = trunc(a#)


Quote: "
'val()' generally deals with string to integer and another detail to be aware of there is the distinction between integer and float. While in DBPro, you could use 'val()' to retrieve a numerical value for either an integer or a float, in AGK/S, you need to use 'val()' for integers and 'valfloat()' for floats."


My memory is not what it used to be, but historically for me int() was used for converting stings to integers and str() to convert the integers to strings.

I have trouble remembering the rest because I rarely need to convert integers to floats and vice versa.

I was not aware of the valfloat(), but I probably would have just converted the float to an integer, and then use val().

Nice to know it is there though as that would give better results of course if you need retain the values past the decimal point.

I rarely need to use floats for things that will be converted to strings for display, so I probably have used the valfloat() before, but I just don't remember it.

Flip flopping between languages, I usually end up a documentation junkie having to refer to the docs a lot as so many languages are so similar but also very different.

Sometimes those subtle differences ruin any convenience of the similarities for me, as I get the commands all mixed up all the time.

PHP puts the $ in front of the variable name for a string where AppGameKit puts it behind, and that was hard for me to get used to. (I prefer the AppGameKit way for that)

Most of the modern languages are alike enough to remember all the commonly used commands and structure though, thanks to C and a few others.

Languages like PHP really irritate me though having to put those stupid semicolons at the end of every line. (I still forget to do that all the time)

Coding things my way since 1981 -- Currently using AppGameKit V2 Tier 1
SFSW
21
Years of Service
User Offline
Joined: 9th Oct 2002
Location:
Posted: 12th Apr 2020 00:07 Edited at: 12th Apr 2020 00:20
Moving between languages can cause confusion, especially if the labels are the same, but the operations are different

Scraggle wrote: "The question was asking for an equivalent of int()
I stand by my answer of b = a#"


In reference to the original post and compared to DBPro and other language behavior:

'int(#)' is not the same as 'b=a#' in AGK/S.

In DBPro and others, 'int(1.52)' will return '1'. So where 'a#=1.52', then 'int(a#)' will return '1'.

In AGK/S however, where 'a#=1.52', then 'b=a#' will return '2'.

These are not the same and the rounding in AGK/S is different in that it is mathematical, not programmatic or truncated as it can be in other languages. So if you try to round in AppGameKit by using 'b=a#', you will not truncate the value down to the lowest integer as you will in other languages. You will round to the nearest integer mathematically. For this reason, it is not the equivalent of 'int(#)' in DBPro or other languages that use truncated down rounding of integers with the function.

So be aware that you will not round in AGK/S using 'b=a#' in the same way as 'int(#)' functions in DBPro or other languages. If you need original programmatic integer down rounding (truncating), you will need to use 'trunc(#)' in AGK/S. You will not get the same result with 'b=a#'. You can print such calculations and values to show this, just put '1.52' in 'a#' and print 'b' to the screen. In AGK/S, you will see '2' appear, not '1'.

This can be an important distinction as it can really throw off code routines you put together where you expect truncated rounding rather than mathematical rounding. Your equations/calculations will need to account for this variation and use AGK/S's method.

Conjured Entertainment wrote: "Languages like PHP really irritate me though having to put those stupid semicolons at the end of every line. (I still forget to do that all the time) "


Semi-colons can indeed be fun I still stumble with that from time to time when writing shaders also.

Login to post a reply

Server time is: 2024-04-25 16:47:08
Your offset time is: 2024-04-25 16:47:08