Author Topic: Is there a better way to force floating point arithmetic?  (Read 294 times)

snowsnowsnow

  • Sr. Member
  • ****
  • Posts: 317
Is there a better way to force floating point arithmetic?
« on: January 04, 2020, 06:07:35 pm »
Consider a simple program like this:

Code: Winbatch
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
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
Message("",(Param1+0.0)/4)
 

Is that any better?

JTaylor

  • Pundit
  • *****
  • Posts: 1362
    • Data & Stuff Inc.
Re: Is there a better way to force floating point arithmetic?
« Reply #1 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

snowsnowsnow

  • Sr. Member
  • ****
  • Posts: 317
Re: Is there a better way to force floating point arithmetic?
« Reply #2 on: January 05, 2020, 12:31:39 am »
What is ObjectType()  ?

Don't think that exists in my version of WB.

stanl

  • Pundit
  • *****
  • Posts: 1182
Re: Is there a better way to force floating point arithmetic?
« Reply #3 on: January 05, 2020, 02:54:56 am »
[see attachment]

snowsnowsnow

  • Sr. Member
  • ****
  • Posts: 317
Re: Is there a better way to force floating point arithmetic?
« Reply #4 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).

JTaylor

  • Pundit
  • *****
  • Posts: 1362
    • Data & Stuff Inc.
Re: Is there a better way to force floating point arithmetic?
« Reply #5 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