Skip to content

Commit

Permalink
Add hypot function
Browse files Browse the repository at this point in the history
  • Loading branch information
LadyCailin committed Feb 28, 2019
1 parent b1853e2 commit c483aea
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/main/java/com/laytonsmith/core/functions/Math.java
Expand Up @@ -2777,6 +2777,64 @@ public ExampleScript[] examples() throws ConfigCompileException {
new ExampleScript("Reverse mode, clamping to minimum due to equal distance", "clamp(15, 20, 10);")
};
}
}

@api
public static class hypot extends CompositeFunction {

@Override
protected String script() {
return "if(double(@arguments[0]) <= 0 || double(@arguments[1]) <= 0) {"
+ "throw(RangeException, 'The values passed to hypot may not be negative');"
+ "}"
+ "return(sqrt((@arguments[0] ** 2) + (@arguments[1] ** 2)));";
}

@Override
public Class<? extends CREThrowable>[] thrown() {
return new Class[]{CRERangeException.class, CRECastException.class};
}

@Override
public boolean isRestricted() {
return false;
}

@Override
public Boolean runAsync() {
return null;
}

@Override
public String getName() {
return "hypot";
}

@Override
public Integer[] numArgs() {
return new Integer[]{2};
}

@Override
public String docs() {
return "number {a, b} Given two sides of a right triangle, returns the length of the hypotenuse, using the"
+ " equation a² + b² = c², where a and b are the arguments provided.";
}

@Override
public Version since() {
return MSVersion.V3_3_4;
}

@Override
public ExampleScript[] examples() throws ConfigCompileException {
return new ExampleScript[]{
new ExampleScript("Standard usage", "hypot(3, 4)"),
new ExampleScript("Standard usage", "hypot(1, 1)"),
new ExampleScript("Standard usage", "hypot(2.5, 4.6)"),
new ExampleScript("Values may not be negative", "hypot(-1, -1)", true)
};
}

}

Expand Down

0 comments on commit c483aea

Please sign in to comment.