Skip to content

Commit

Permalink
WyRT: Updated standard library for #418.
Browse files Browse the repository at this point in the history
  • Loading branch information
DavePearce committed Sep 5, 2014
1 parent 0bf8c5c commit d27bfb4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion modules/wyrt/src/whiley/lang/Int.whiley
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public function parse(string input) => int
r = r * 10
if !Char.isDigit(c):
throw SyntaxError("invalid number string",i,i)
r = r + (c - '0')
r = r + ((int) c - '0')
// done
if negative:
return -r
Expand Down
22 changes: 11 additions & 11 deletions modules/wyrt/src/whiley/lang/Math.whiley
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ ensures x < 0 ==> r == -x:
*/
public function abs(real x) => (real r)
// if input positive, then result equals input
ensures x >= 0 ==> r == x
ensures x >= 0.0 ==> r == x
// if input negative, then result equals negated input
ensures x < 0 ==> r == -x:
ensures x < 0.0 ==> r == -x:
//
if x < 0:
if x < 0.0:
return -x
else:
return x
Expand Down Expand Up @@ -127,15 +127,15 @@ requires exponent > 0:
*/
public function floor(real x) => (int r)
// Return is greater-than-or-equal to input
ensures r <= x
ensures ((real) r) <= x
// Input value is between return and return plus one
ensures r + 1 > x:
ensures ((real) r + 1) > x:
//
int num
int den
num/den = x
int r = num / den
if x < 0 && den != 1:
if x < 0.0 && den != 1:
return r - 1
else:
return r
Expand All @@ -146,15 +146,15 @@ ensures r + 1 > x:
*/
public function ceil(real x) => (int r)
// Return is greater-than-or-equal to input
ensures x <= r
ensures x <= ((real) r)
// Input value is between return and return less one
ensures r - 1 < x:
ensures ((real) r - 1) < x:
//
int num
int den
num/den = x
int r = num / den
if x > 0 && den != 1:
if x > 0.0 && den != 1:
return r + 1
else:
return r
Expand All @@ -165,9 +165,9 @@ ensures r - 1 < x:
*/
public function round(real x) => (int r)
// Difference between input and return is 0.5 or less
ensures max(x,r) - min(x,r) <= 0.5:
ensures max(x,(real) r) - min(x, (real) r) <= 0.5:
//
if x >= 0:
if x >= 0.0:
return floor(x + 0.5)
else:
return ceil(x - 0.5)
Expand Down
14 changes: 7 additions & 7 deletions modules/wyrt/src/whiley/lang/Real.whiley
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ throws SyntaxError:
throw SyntaxError("invalid number string",i,i)
else:
r = r * 10
r = r + (c - '0')
r = r + (int) (c - '0')
dps = dps * 10
// finally, perform division
real rr = (real) r
if dps > 0:
return rr / dps
return rr / (real) dps
else:
return rr

Expand All @@ -63,21 +63,21 @@ public function toDecimal(real x) => string:
// the following is a convenience method.
public function toDecimal(real x, int ndigits) => string:
string r
if x < 0:
if x < 0.0:
r = "-"
x = -x
else:
r = ""
int n / int d = x
int digit = n / d
real rem = x - digit
real rem = x - (real) digit
r = r ++ digit ++ "."
int i = 1
while i < ndigits && rem != 0:
rem = rem * 10
while i < ndigits && rem != 0.0:
rem = rem * 10.0
n / d = rem
digit = n / d
rem = rem - digit
rem = rem - (real) digit
r = r ++ digit
i = i + 1
// need to support rounding!
Expand Down
2 changes: 1 addition & 1 deletion modules/wyrt/src/whiley/lang/String.whiley
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,6 @@ public function toUTF8(string s) => [byte]:
[byte] r = []
for c in s:
// the following line is fatally flawed!
r = r ++ [Int.toUnsignedByte(c)]
r = r ++ [Int.toUnsignedByte((int) c)]
return r

0 comments on commit d27bfb4

Please sign in to comment.