Skip to content

Commit

Permalink
Merge pull request #10 from ProHolz/master
Browse files Browse the repository at this point in the history
Added class method Math.Round(a : Double; digits :  Integer) : Double;
  • Loading branch information
dwarfland committed Feb 4, 2019
2 parents 7dce06d + b4a3c7c commit ff9a66d
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Source/Convert.pas
Expand Up @@ -287,9 +287,9 @@ implementation

method Convert.TrimLeadingZeros(aValue: not nullable String): not nullable String;
begin
for i: Int32 := 0 to length(aValue)-2 do
for i: Int32 := 0 to length(aValue)-1 do
if aValue[i] ≠ '0' then exit aValue.Substring(i);
exit aValue;
exit "";
end;

method Convert.ToHexString(aValue: UInt64; aWidth: Integer := 0): not nullable String;
Expand Down
73 changes: 58 additions & 15 deletions Source/Math.pas
Expand Up @@ -37,6 +37,7 @@ interface
class method Min(a,b: Int64): Int64; mapped to min(a,b);
class method Pow(x, y: Double): Double;
class method Round(a: Double): Int64;
class method Round(a : Double; digits : Integer) : Double;
class method Sign(d: Double): Integer;
class method Sin(d: Double): Double; mapped to sin(d);
class method Sinh(d: Double): Double; mapped to sinh(d);
Expand Down Expand Up @@ -72,6 +73,7 @@ interface
class method Min(a,b: Int64): Int64;
class method Pow(x, y: Double): Double;
class method Round(a: Double): Int64;
class method Round(a : Double; digits : Integer) : Double;
class method Sign(d: Double): Integer;
class method Sin(x: Double): Double;
class method Sinh(x: Double): Double;
Expand Down Expand Up @@ -106,6 +108,7 @@ interface
class method Min(a,b: Int64): Int64; mapped to Min(a,b);
class method Pow(x, y: Double): Double; mapped to Pow(x,y);
class method Round(a: Double): Int64;
class method Round(a : Double; digits : Integer) : Double;
class method Sign(d: Double): Integer; mapped to Sign(d);
class method Sin(d: Double): Double; mapped to Sin(d);
class method Sinh(d: Double): Double; mapped to Sinh(d);
Expand Down Expand Up @@ -238,25 +241,19 @@ implementation

exit Int64(Floor(a + 0.499999999999999999));
end;
{$ELSEIF TOFFEE}
class method Math.Round(a: Double): Int64;
begin
if Consts.IsNaN(a) or Consts.IsInfinity(a) then
raise new ArgumentException("Value can not be rounded to Int64");

exit Int64(Floor(a + 0.499999999999999999));
end;
{$ELSEIF ECHOES or ISLAND}
class method Math.Round(a: Double): Int64;
class method Math.Round(a : Double; digits : Integer) : Double;
begin
if Consts.IsNaN(a) or Consts.IsInfinity(a) then
raise new ArgumentException("Value can not be rounded to Int64");

exit Int64(Floor(a + 0.499999999999999999));
if (digits < 0) or (digits > 15) then
raise new ArgumentException("digits must be between 0 and 15");
var factor := Pow(10.0, -digits);
if a > 0 then
result := Truncate((a / factor)+0.5) * factor
else
result := Truncate((a / factor)-0.5) * factor
end;
{$ENDIF}

{$IF COOPER}

class method Math.Sign(d: Double): Integer;
begin
if Consts.IsNaN(d) then
Expand All @@ -266,6 +263,7 @@ implementation
if d < 0 then exit -1;
exit 0;
end;

{$ELSEIF TOFFEE}
class method Math.Sign(d: Double): Integer;
begin
Expand Down Expand Up @@ -434,6 +432,51 @@ implementation
begin
exit iif(a < b, a, b);
end;

class method Math.Round(a: Double): Int64;
begin
if Consts.IsNaN(a) or Consts.IsInfinity(a) then
raise new ArgumentException("Value can not be rounded to Int64");

exit Int64(Floor(a + 0.499999999999999999));
end;

class method Math.Round(a : Double; digits : Integer) : Double;
begin
if (digits < 0) or (digits > 15) then
raise new ArgumentException("digits must be between 0 and 15");
var factor := Pow(10.0, -digits);
if a > 0 then
result := Truncate((a / factor)+0.5) * factor
else
result := Truncate((a / factor)-0.5) * factor
end;

{$ELSEIF ECHOES OR ISLAND}
class method Math.Round(a: Double): Int64;
begin
if Consts.IsNaN(a) or Consts.IsInfinity(a) then
raise new ArgumentException("Value can not be rounded to Int64");

exit Int64(Floor(a + 0.499999999999999999));
end;

class method Math.Round(a : Double; digits : Integer) : Double;
begin
{$IF ECHOES}
result := mapped.Round(a, digits);
exit;
{$ELSE}
if (digits < 0) or (digits > 15) then
raise new ArgumentException("digits must be between 0 and 15");
var factor := Pow(10.0, -digits);
if a > 0 then
result := Truncate((a / factor)+0.5) * factor
else
result := Truncate((a / factor)-0.5) * factor
{$ENDIF}
end;

{$ENDIF}

end.
32 changes: 32 additions & 0 deletions Test/Convert.pas
Expand Up @@ -8,6 +8,14 @@
ConvertTests = public class(Test)
private
protected
// Copy from Convert.... because in Convert it is private and inline
method internTrimLeadingZeros(aValue: not nullable String): not nullable String;
begin
for i: Int32 := 0 to length(aValue)-1 do
if aValue[i] ≠ '0' then exit aValue.Substring(i);
exit "";
end;

public

method TestInt32;
Expand Down Expand Up @@ -68,6 +76,30 @@
//Check.AreEqual(Convert.TryToInt32("9223372036854775807"), 9223372036854775807); // fails to compile: // E546 Value "9223372036854775807" exceeds the bounds of target type "Int32"
end;


method TestHex;
begin
Assert.AreEqual(Convert.ToHexString(10,0), "A");
end;



method TrimLeadingZeros;
begin
var res : String;
var aValue := "000000000000000A";
res := internTrimLeadingZeros(aValue);
Assert.AreEqual(res, "A");
aValue := "00000000000000A0B";
res := internTrimLeadingZeros(aValue);
Assert.AreEqual(res, "A0B");

aValue := "0000000000000000";
res := internTrimLeadingZeros(aValue);
Assert.AreEqual(res, "");

end;

end;

end.
41 changes: 41 additions & 0 deletions Test/Math.pas
@@ -0,0 +1,41 @@
namespace Elements.RTL2.Tests.Shared;

interface

uses
RemObjects.Elements.RTL,
RemObjects.Elements.EUnit;

type
MathTest = public class(Test)
private
protected
public
method RoundTest;
end;

implementation

method MathTest.RoundTest;
begin
// const PI: Double = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679;
var a : Double := 3.141592653589793;//2384626433832795028841971693993751058209749445923078164062862089986280348253421170679;

Assert.DoesNotThrows(()-> Math.Round(a));
Assert.AreEqual(Math.Round(a), 3);

{$if ECHOES or TOFFEE or ISLAND or COOPER}
Assert.Throws(()-> Math.Round(a,-1));
Assert.Throws(()-> Math.Round(a,16));
Assert.AreEqual(Math.Round(a,0), 3.0);
Assert.AreEqual(Math.Round(a,4), 3.1416);
Assert.AreEqual(Math.Round(a,9), 3.141592654);
Assert.AreEqual(Math.Round(a,12), 3.141592653590);
Assert.AreEqual(Math.Round(a,13), 3.1415926535898);
Assert.AreEqual(Math.Round(a,14), 3.14159265358979);
Assert.AreEqual(Math.Round(a,15), 3.141592653589793);
{$ENDIF}

end;

end.

0 comments on commit ff9a66d

Please sign in to comment.