WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: snowsnowsnow on January 04, 2020, 06:07:35 PM

Title: Is there a better way to force floating point arithmetic?
Post by: snowsnowsnow on January 04, 2020, 06:07:35 PM
Consider a simple program like this:

Code (winbatch) Select

Message("",Param1/4)


So you run this with a parameter of 3.  You get back 0 instead of .75

(Obviously, this example is contrived, but it springs from a real world problem)

The only workaround I could find is:

Code (winbatch) Select

Message("",%Param1%.0/4)


It seems to me there should be a more straightforward (less ugly) way to force it to do decimal math.  Is there?

Edit: I just thought of this.  Would this work?

Code (winbatch) Select

Message("",(Param1+0.0)/4)


Is that any better?
Title: Re: Is there a better way to force floating point arithmetic?
Post by: JTaylor on January 04, 2020, 06:25:13 PM
I often do something like that but there is also...

num = ObjectType("R4",7)
message("HEY",num/4)

Jim
Title: Re: Is there a better way to force floating point arithmetic?
Post by: snowsnowsnow on January 05, 2020, 12:31:39 AM
What is ObjectType()  ?

Don't think that exists in my version of WB.
Title: Re: Is there a better way to force floating point arithmetic?
Post by: stanl on January 05, 2020, 02:54:56 AM
[see attachment]
Title: Re: Is there a better way to force floating point arithmetic?
Post by: snowsnowsnow on January 05, 2020, 08:24:42 AM
It just seems like there should be a way to just say:

Evaluate this string in floating point.

without the kludgieness of any of the "solutions" we've seen so far in this thread (many of them by me).
Title: Re: Is there a better way to force floating point arithmetic?
Post by: JTaylor on January 05, 2020, 11:46:09 AM
You can.   That is the purpose of ObjectType().   You just tell it a variable is explicitly a floating point and it will treat that way from that point on.  It is simply the trade-off for the automagical conversion ability of WinBatch but really no different than strongly typed languages where you have to explicitly type them for every definition.   WinBatch just uses the ObjectType syntax for the typing when you want it explicit but makes it easier for everything else.

Jim