Skip to content

Commit

Permalink
Merge pull request #761 from ePaul/feature/add-Integer.nearestFloat
Browse files Browse the repository at this point in the history
Issue #759: Add attribute Integer.nearestFloat.
  • Loading branch information
chochos committed Sep 21, 2015
2 parents 8870a00 + 80dd26b commit 0e04a41
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions runtime-js/Integer/nearestFloat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return Float(this.valueOf());
9 changes: 9 additions & 0 deletions runtime/ceylon/language/Integer.java
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,15 @@ public static double getFloat(long value) {
}
}

public double getNearestFloat() {
return (double) value;
}

@Ignore
public static double getNearestFloat(long value) {
return (double) value;
}

public byte getByte() {
return getByte(value);
}
Expand Down
12 changes: 11 additions & 1 deletion src/ceylon/language/Integer.ceylon
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,17 @@ shared native final class Integer(Integer integer)
this.magnitude>runtime.maxExactIntegralFloat")
see (`value runtime.maxExactIntegralFloat`)
shared native Float float;


"The nearest [[Float]] to this number. For
Integers in magnitude less than 2^53, this is always
a Float with the exact same mathematical value (and the
same value as [[float]]), for larger ones on the JVM the
Floats are less dense than the Integers so there can be some
loss of precision.
This method never throws an OverflowException."
shared native Float nearestFloat;

shared actual native Integer predecessor;
shared actual native Integer successor;
shared actual native Integer neighbour(Integer offset);
Expand Down
14 changes: 14 additions & 0 deletions test/numbers.ceylon
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,20 @@ shared void numbers() {
check((-1.1).integer==-1, "(-1.1).integer is `` (-1.1).integer `` instead of -1");
check(2.float==2.0, "integer float");
check((-3).float==-3.0, "negative integer float");
check(2.nearestFloat==2.0, "small integer nearestFloat");
check((-3).nearestFloat==-3.0, "small negative integer nearestFloat");



try {
fail("Should have thrown Overflow Exception: ``922337203685477632.float``");
} catch(OverflowException ex) {
check(ex.message == "922337203685477632 cannot be coerced into a 64 bit floating point value");
}

check(922337203685477632.nearestFloat == 9.2233720368547763E17, "large nearest float 1");
check((-922337203685477632).nearestFloat == -9.2233720368547763E17, "large negative nearest float");
check(922337203685477635.nearestFloat == 9.2233720368547763E17, "large nearest float 2");

check(1.plus { other=2; }.equals { that=3; }, "natural named args");

Expand Down

0 comments on commit 0e04a41

Please sign in to comment.