[FREQ] Math Functions

If you have an idea for a new feature, please discuss it in the main MapTool forum first, then post a summary of the discussion here. Use the first Sticky as a template.

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

User avatar
JamzTheMan
Great Wyrm
Posts: 1872
Joined: Mon May 10, 2010 12:59 pm
Location: Chicagoland
Contact:

Re: [FREQ] Math Functions

Post by JamzTheMan »

Hmm, they seem to all be under the Parser project as individual classes:

parser\src\main\java\net\rptools\parser\function\impl
AbsoluteValue.java

Code: Select all

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package net.rptools.parser.function.impl;

import java.math.BigDecimal;
import java.util.List;

import net.rptools.parser.Parser;
import net.rptools.parser.function.AbstractNumberFunction;

public class AbsoluteValue extends AbstractNumberFunction {
    public AbsoluteValue() {
        super(1, 1, "abs", "absolutevalue");
    }
    
    @Override
    public Object childEvaluate(Parser parser, String functionName, List<Object> parameters) {
        BigDecimal value = (BigDecimal) parameters.get(0); 
        
        return value.abs();
    }
}
Hypotenuse.java

Code: Select all

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package net.rptools.parser.function.impl;

import java.math.BigDecimal;
import java.util.List;

import net.rptools.parser.Parser;
import net.rptools.parser.function.AbstractNumberFunction;
import net.rptools.parser.function.EvaluationException;
import net.rptools.parser.function.ParameterException;


public class Hypotenuse extends AbstractNumberFunction {
    public Hypotenuse() {
        super(2, 2, "hypot", "hypotenuse");
    }
    
    @Override
    public Object childEvaluate(Parser parser, String functionName, List<Object> parameters) throws EvaluationException, ParameterException {
        BigDecimal value1 = (BigDecimal) parameters.get(0); 
        BigDecimal value2 = (BigDecimal) parameters.get(1);
        
        return new BigDecimal(Math.hypot(value1.doubleValue(), value2.doubleValue()));
    }
}
etc...

I would just create a new class called MathFunctions.java and put them in one place under:
maptool\maptool\src\main\java\net\rptools\maptool\client\functions

If Craig or others want it under the Parser they can move em easily enough and/or break it up into individual classes.
-Jamz
____________________
Custom MapTool 1.4.x.x Fork: maptool.nerps.net
Custom TokenTool 2.0 Fork: tokentool.nerps.net
More information here: MapTool Nerps! Fork

User avatar
RPTroll
TheBard
Posts: 3159
Joined: Tue Mar 21, 2006 7:26 pm
Location: Austin, Tx
Contact:

Re: [FREQ] Math Functions

Post by RPTroll »

This is now 15.9 in the tracking spreadsheet with d4rkAlf as the assignee.
ImageImage ImageImageImageImage
Support RPTools by shopping
Image
Image

d4rkAlf
Cave Troll
Posts: 35
Joined: Thu Apr 16, 2015 9:48 am

Re: [FREQ] Math Functions

Post by d4rkAlf »

RPTroll wrote:This is now 15.9 in the tracking spreadsheet with d4rkAlf as the assignee.
Sounds good. I'm going to wait with implementing it until the parser has been moved to Github. That way I can add them where they belong and nobody has to move them to the correct place in the future.

User avatar
Jagged
Great Wyrm
Posts: 1306
Joined: Mon Sep 15, 2008 9:27 am
Location: Bristol, UK

Re: [FREQ] Math Functions

Post by Jagged »

Can I request that any trigonometric functions do NOT default to radians.

<Muttley>snassel fassel</Muttley>

d4rkAlf
Cave Troll
Posts: 35
Joined: Thu Apr 16, 2015 9:48 am

Re: [FREQ] Math Functions

Post by d4rkAlf »

Jagged wrote:Can I request that any trigonometric functions do NOT default to radians.

<Muttley>snassel fassel</Muttley>
Yeah, getTokenFacing defaults to using degrees so it would make the most sense that new trigonometric functions do so as well.

User avatar
RPTroll
TheBard
Posts: 3159
Joined: Tue Mar 21, 2006 7:26 pm
Location: Austin, Tx
Contact:

Re: [FREQ] Math Functions

Post by RPTroll »

+1
ImageImage ImageImageImageImage
Support RPTools by shopping
Image
Image

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: [FREQ] Math Functions

Post by wolph42 »

let me repeat myself:
wolph42 wrote: i'd prefer separate functions. and while we're on it: a parameter to choose between radians and degrees (defaulting to degrees).
So

Code: Select all

sin(30[, useRadians])
Functions:
sin, cos, tan, cotan, pi, degrees2radians, radians2degrees.

other useful math functions are:
mod, abs, odd (or isOdd), div.

User avatar
RPTroll
TheBard
Posts: 3159
Joined: Tue Mar 21, 2006 7:26 pm
Location: Austin, Tx
Contact:

Re: [FREQ] Math Functions

Post by RPTroll »

I'm trying (and failing) to think of a scenario in MapTool when I would actually need radians.
ImageImage ImageImageImageImage
Support RPTools by shopping
Image
Image

User avatar
Jagged
Great Wyrm
Posts: 1306
Joined: Mon Sep 15, 2008 9:27 am
Location: Bristol, UK

Re: [FREQ] Math Functions

Post by Jagged »

Especially as token facing is in degrees.

Craig
Great Wyrm
Posts: 2107
Joined: Sun Jun 22, 2008 7:53 pm
Location: Melbourne, Australia

Re: [FREQ] Math Functions

Post by Craig »

RPTroll wrote:I'm trying (and failing) to think of a scenario in MapTool when I would actually need radians.
Because Radians are rad man!
Seriously though, if you want to perform any type of math you will need to do so in radians so why force people to convert back and forth. I don't know what people want to do with it, but I am sure some wants it for something similarly there should be functions to get the facing in radians. Maybe instead of having an option parameter we just have _r e.g. sin_r(), cos_r(), get_facing_r() etc...

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: [FREQ] Math Functions

Post by wolph42 »

Craig wrote:Seriously though, if you want to perform any type of math you will need to do so in radians
hmm, I'm inclined to say 'if you want to perform any *serious* type of math'. For the usual (tri)angle calculation we are confronted with in MT, there is little use for radians. They only have meaning if get/setfacing is also in radians and unfortunately they are not intuitive for 90% of the population so although they are better, in this case they're less preferred.

Nonetheless i'm of the opinion that both should be provided, hence my suggestion for the option. Seperate functions as craig suggests is fine by me as well.

Post Reply

Return to “Feature Requests”