|
12 | 12 | import java.io.IOException; |
13 | 13 | import java.math.BigDecimal; |
14 | 14 | import java.math.BigInteger; |
| 15 | +import java.math.MathContext; |
15 | 16 | import java.math.RoundingMode; |
16 | 17 | import java.nio.file.Files; |
17 | 18 | import java.nio.file.Path; |
@@ -741,22 +742,22 @@ else if (ast.size() > 2) |
741 | 742 | return DList.valueOf(result); |
742 | 743 | }), 2, false); |
743 | 744 | dEnv.addTokenFunction("range", ((args, env) -> { |
744 | | - if (!(args.getFirst() instanceof DInt)) |
745 | | - throw new DevoreCastException(args.getFirst().type(), "int"); |
746 | | - if (args.size() > 1 && !(args.get(1) instanceof DInt)) |
747 | | - throw new DevoreCastException(args.get(1).type(), "int"); |
748 | | - if (args.size() > 2 && !(args.get(2) instanceof DInt)) |
749 | | - throw new DevoreCastException(args.get(2).type(), "int"); |
750 | | - BigInteger start = args.size() > 1 ? ((DInt) args.getFirst()).toBigInteger() : BigInteger.ZERO; |
751 | | - BigInteger end = args.size() > 1 ? ((DInt) args.get(1)).toBigInteger().subtract(BigInteger.ONE) |
752 | | - : ((DInt) args.getFirst()).toBigInteger().subtract(BigInteger.ONE); |
753 | | - BigInteger step = args.size() > 2 ? ((DInt) args.get(2)).toBigInteger() : BigInteger.ONE; |
754 | | - BigInteger size = end.subtract(start).divide(step); |
| 745 | + if (!(args.getFirst() instanceof DNumber)) |
| 746 | + throw new DevoreCastException(args.getFirst().type(), "number"); |
| 747 | + if (args.size() > 1 && !(args.get(1) instanceof DNumber)) |
| 748 | + throw new DevoreCastException(args.get(1).type(), "number"); |
| 749 | + if (args.size() > 2 && !(args.get(2) instanceof DNumber)) |
| 750 | + throw new DevoreCastException(args.get(2).type(), "number"); |
| 751 | + BigDecimal start = args.size() > 1 ? ((DNumber) args.getFirst()).toBigDecimal() : BigDecimal.ZERO; |
| 752 | + BigDecimal end = args.size() > 1 ? ((DNumber) args.get(1)).toBigDecimal().subtract(BigDecimal.ONE) |
| 753 | + : ((DNumber) args.getFirst()).toBigDecimal().subtract(BigDecimal.ONE); |
| 754 | + BigDecimal step = args.size() > 2 ? ((DNumber) args.get(2)).toBigDecimal() : BigDecimal.ONE; |
| 755 | + BigDecimal size = end.subtract(start).divide(step, MathContext.DECIMAL128); |
755 | 756 | List<Token> list = new ArrayList<>(); |
756 | | - BigInteger i = BigInteger.ZERO; |
| 757 | + BigDecimal i = BigDecimal.ZERO; |
757 | 758 | while (i.compareTo(size) < 1) { |
758 | | - list.add(DInt.valueOf(start.add(i.multiply(step)))); |
759 | | - i = i.add(BigInteger.ONE); |
| 759 | + list.add(DFloat.valueOf(start.add(i.multiply(step)))); |
| 760 | + i = i.add(BigDecimal.ONE); |
760 | 761 | } |
761 | 762 | return DList.valueOf(list); |
762 | 763 | }), 1, true); |
|
0 commit comments