Skip to content

Commit

Permalink
add Int.clamp
Browse files Browse the repository at this point in the history
  • Loading branch information
chunquedong committed Dec 11, 2021
1 parent 36f0e2c commit 7d6778c
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 5 deletions.
8 changes: 8 additions & 0 deletions doc/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
## ChangeLog

### Build 4.1.4 (2021-12-11)
- fix local var scpe start
- fix Type toImmutable
- fix read FFI fcode error
- fix negative wait time
- fix enclosingVars
- Int.clamp,Float.clamp

### Build 4.1.3 (2021-10-12)
- native: fix Method reflect with default param
- native: Pod.file fallback
Expand Down
4 changes: 0 additions & 4 deletions library/std/fan/io/File.fan
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ abstract const class File
File.make(path.toUri, checkSlash)
}

static File fromOsPath(Str path) {
File.os(path)
}

**
** Make a File for the specified operating system specific path
** on the local file system.
Expand Down
8 changes: 8 additions & 0 deletions library/std/fan/util/Math.fan
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,17 @@ native mixin Math

**
** Clip this float between the min and max. If its less than min then
** Clamp this float between the min and max. If its less than min then
** return min, if its greater than max return max, otherwise return this
** float itself.
**
static extension Float clamp(Float self, Float min, Float max) {
if (self < min) return min
if (self > max) return max
return self
}

@NoDoc @Deprecated { msg = "Use clamp" }
static extension Float clip(Float self, Float min, Float max) {
if (self < min) return min
if (self > max) return max
Expand Down
7 changes: 7 additions & 0 deletions library/std/java/src/fan/std/Math$.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ public static double max(double self, double that) {
return that;
}

public static double clamp(double self, double min, double max)
{
if (self < min) return min;
if (self > max) return max;
return self;
}

public static double clip(double self, double min, double max)
{
if (self < min) return min;
Expand Down
6 changes: 6 additions & 0 deletions library/std/js/Math.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ fan.std.Math.log = function(self) { return fan.sys.Float.make(Math.log(self));
fan.std.Math.log10 = function(self) { return fan.sys.Float.make(Math.log(self) / Math.LN10); }
fan.std.Math.min = function(self, that) { return fan.sys.Float.make(Math.min(self, that)); }
fan.std.Math.max = function(self, that) { return fan.sys.Float.make(Math.max(self, that)); }
fan.std.Math.clamp = function(self, min, max)
{
if (self < min) return min;
if (self > max) return max;
return self;
}
fan.std.Math.clip = function(self, min, max)
{
if (self < min) return min;
Expand Down
9 changes: 8 additions & 1 deletion library/sys/fan/native/Int.fan
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,17 @@ native const struct class Int : Num
Int max(Int that) { this > that ? this : that }

**
** Clip this integer between the min and max. If its less than min then
** Clamp this integer between the min and max. If its less than min then
** return min, if its greater than max return max, otherwise return this
** integer itself.
**
Int clamp(Int min, Int max) {
if (this < min) return min
if (this > max) return max
return this
}

@NoDoc @Deprecated { msg = "Use clamp" }
Int clip(Int min, Int max) {
if (this < min) return min
if (this > max) return max
Expand Down
7 changes: 7 additions & 0 deletions library/sys/java/fan/sys/FanInt.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ public static long max(long self, long that)
return that;
}

public static long clamp(long self, long min, long max)
{
if (self < min) return min;
if (self > max) return max;
return self;
}

public static long clip(long self, long min, long max)
{
if (self < min) return min;
Expand Down
6 changes: 6 additions & 0 deletions library/sys/js/Int.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ fan.sys.Int.hash = function(self) { return self; }
fan.sys.Int.abs = function(self) { return self < 0 ? -self : self; }
fan.sys.Int.min = function(self, val) { return self < val ? self : val; }
fan.sys.Int.max = function(self, val) { return self > val ? self : val; }
fan.sys.Int.clamp = function(self, min, max)
{
if (self < min) return min;
if (self > max) return max;
return self;
}
fan.sys.Int.clip = function(self, min, max)
{
if (self < min) return min;
Expand Down

0 comments on commit 7d6778c

Please sign in to comment.