-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
9ec7415
commit 4f71673
Showing
5 changed files
with
454 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...c/main/java/org/enso/interpreter/node/expression/builtin/number/decimal/TruncateNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.enso.interpreter.node.expression.builtin.number.decimal; | ||
|
||
import com.oracle.truffle.api.CompilerDirectives; | ||
import com.oracle.truffle.api.nodes.Node; | ||
import com.oracle.truffle.api.profiles.ConditionProfile; | ||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
import org.enso.interpreter.dsl.BuiltinMethod; | ||
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps; | ||
import org.enso.interpreter.runtime.number.EnsoBigInteger; | ||
|
||
@BuiltinMethod( | ||
type = "Decimal", | ||
name = "truncate_builtin", | ||
description = "Truncate a floating-point number to an integer by dropping the fractional part.") | ||
public class TruncateNode extends Node { | ||
private final ConditionProfile fitsProfile = ConditionProfile.createCountingProfile(); | ||
|
||
Object execute(double self) { | ||
if (fitsProfile.profile(BigIntegerOps.fitsInLong(self))) { | ||
return (long) self; | ||
} else { | ||
return new EnsoBigInteger(toBigInteger(self)); | ||
} | ||
} | ||
|
||
@CompilerDirectives.TruffleBoundary | ||
private static BigInteger toBigInteger(double self) { | ||
return BigDecimal.valueOf(self).toBigIntegerExact(); | ||
} | ||
} |
Oops, something went wrong.