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.

AppGameKit Classic Chat / [SOLVED] assigning the result of integer division to a float

Author
Message
Dale Schultz
2
Years of Service
User Offline
Joined: 1st Nov 2021
Location: Maine, USA
Posted: 3rd Nov 2021 02:16
I do expect the result of integer division to be a real number when assigned to a float.

What we find though is:



This is especially annoying when both values come in from integer functions, for example:



So I made a helper function that will produce a float with 4 decimal places



so now:



Is there a better way? Perhaps, I have not found any way to cast an integer to a float so perhaps a simple castFloat() will do the trick?



Then we can do:



Is there a built in cast function? Or a better way? Might multiplying and dividing by 8 perhaps be faster than 10?

The author of this post has marked a post as an answer.

Go to answer

Virtual Nomad
Moderator
18
Years of Service
Recently Online
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 3rd Nov 2021 02:31
i generally do
Dale Schultz
2
Years of Service
User Offline
Joined: 1st Nov 2021
Location: Maine, USA
Posted: 3rd Nov 2021 02:46
yes that looks better! thanks
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 3rd Nov 2021 09:33
Quote: "i generally do"


why not just cast to float in the parameters



Quote: "I do expect the result of integer division to be a real number when assigned to a float."


If you want float division then divide floats, not ints, the code is doing exactly what you are telling it to do



Quote: "This is especially annoying when both values come in from integer functions, for example:"


Fair point, first, cast the ints to floats, then divide or use a wrapper function that casts the ints to floats (as above), no need for mathematical code gymnastics.


inline casting would be nice but AppGameKit script is not that advanced, it really is BASIC! , if you want that amount of control over variable types you are going to need Tier2 and the power of C++








Open Source plugins
Cl - DnD Plugin
Buy Me A Coffee
Virtual Nomad
Moderator
18
Years of Service
Recently Online
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 3rd Nov 2021 12:31
Quote: "why not just cast to float in the parameters"
i like. i haven't picked that habit up yet
Dale Schultz
2
Years of Service
User Offline
Joined: 1st Nov 2021
Location: Maine, USA
Posted: 3rd Nov 2021 12:34
This post has been marked by the post author as the answer.
Quote: "why not just cast to float in the parameters"


That is brilliant!

So this is all one needs:




BTW, I have been using Visual Basic for about 28 years and it has had (exposed) cast functions all along, and is also smart enough to assign a real value that results from integer division if the destination variable is a real type. So this has nothing to do with being Basic.

Visual Basic:

PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 3rd Nov 2021 13:17
Quote: "BTW"

Ah, c'mon ... VB is another beast entirely, I started coding with VB6 and loved it, that ActiveX component system was second to none I just wish I knew more at the time so I could have made better use of it, does it still work like that??, I jumped to PureBasic when MS murdered VB6, so yeah I agree, the emphasis was on the adjective meaning of the word "really is BASIC"

Quote: "So this is all one needs:"


Good idea I didn't think of that

another approach would be to convert it to a string and then to a float, but your solution if much better IMO.


What AppGameKit needs is a ToFloat function

[Poll]
ToFloat() to match ValFloat()?
or
Flo() to match Str()?
[/Poll]

Experienced coders that come to AppGameKit always have a head scratching moment, it has many quirks




Open Source plugins
Cl - DnD Plugin
Buy Me A Coffee
Dale Schultz
2
Years of Service
User Offline
Joined: 1st Nov 2021
Location: Maine, USA
Posted: 3rd Nov 2021 15:02
They did a fantastic job on VB6, stunning IDE and debugger, I still use it and it still works. Being able to call into the Windows system makes everything possible. I have a program I use for controlling a model train layout that is now > 20 years old and I keep adding features. It does text to speech, provides a web server, talks to RemoteSigns, controls Hue lights, makes announcements and can even talk to other copies of itself on the network . I think there are so many apps written in it , MS will continue to ensure it runs.

I have been impressed by AppGameKit too, lots of nice things. Especially assigning an initial value when declaring a variable. Fingers still type End if though.
Broadcast feature is great, very slick.

I do miss the ability to declare vars in blocks like C++ so one can say: for (int i =1 , i< 4, ++i) etc.

Yes, I think a built-in cast function would be good, and will probably perform better then my empty function. They already have the code to do it, just have to expose it more. Function name should be chosen to minimize clashes with existing code functions something like: CastFloat() or CastF() or CFloat()
No, converting to string first probably not efficient and sort of hacky.

Login to post a reply

Server time is: 2024-04-23 15:30:06
Your offset time is: 2024-04-23 15:30:06