mirror of
https://github.com/CatalaLang/catala.git
synced 2024-09-19 16:28:12 +03:00
Merge branch 'master' into allocations_logement
This commit is contained in:
commit
8f68e2b262
@ -97,6 +97,7 @@ type unop =
|
||||
| GetDay
|
||||
| GetMonth
|
||||
| GetYear
|
||||
| RoundMoney
|
||||
|
||||
type operator = Ternop of ternop | Binop of binop | Unop of unop
|
||||
|
||||
|
@ -96,6 +96,7 @@ type unop =
|
||||
| GetDay
|
||||
| GetMonth
|
||||
| GetYear
|
||||
| RoundMoney
|
||||
|
||||
type operator = Ternop of ternop | Binop of binop | Unop of unop
|
||||
|
||||
|
@ -266,6 +266,8 @@ let rec evaluate_operator
|
||||
A.ELit (LInt Runtime.(year_of_date d))
|
||||
| A.Unop A.IntToRat, [ ELit (LInt i) ] ->
|
||||
A.ELit (LRat Runtime.(decimal_of_integer i))
|
||||
| A.Unop A.RoundMoney, [ ELit (LMoney m) ] ->
|
||||
A.ELit (LMoney Runtime.(money_round m))
|
||||
| A.Unop (A.Log (entry, infos)), [ e' ] ->
|
||||
if !Cli.trace_flag then (
|
||||
match entry with
|
||||
|
@ -209,7 +209,8 @@ let format_unop (fmt : Format.formatter) (op : unop Pos.marked) : unit =
|
||||
| IntToRat -> "int_to_rat"
|
||||
| GetDay -> "get_day"
|
||||
| GetMonth -> "get_month"
|
||||
| GetYear -> "get_year")
|
||||
| GetYear -> "get_year"
|
||||
| RoundMoney -> "round_money")
|
||||
|
||||
let needs_parens (e : expr Pos.marked) : bool =
|
||||
match Pos.unmark e with EAbs _ | ETuple (_, Some _) -> true | _ -> false
|
||||
|
@ -196,6 +196,7 @@ let op_type (op : A.operator Pos.marked) : typ Pos.marked UnionFind.elem =
|
||||
| A.Unop A.GetDay -> arr dat it
|
||||
| A.Unop A.GetMonth -> arr dat it
|
||||
| A.Unop A.GetYear -> arr dat it
|
||||
| A.Unop A.RoundMoney -> arr mt mt
|
||||
| A.Unop A.IntToRat -> arr it rt
|
||||
| Binop (Mult (KDate | KDuration)) | Binop (Div KDate) | Unop (Minus KDate) ->
|
||||
Errors.raise_spanned_error pos "This operator is not available!"
|
||||
|
@ -122,6 +122,7 @@ let format_unop (fmt : Format.formatter) (op : Dcalc.Ast.unop Pos.marked) : unit
|
||||
| GetDay -> Format.fprintf fmt "%s" "day_of_month_of_date"
|
||||
| GetMonth -> Format.fprintf fmt "%s" "month_number_of_date"
|
||||
| GetYear -> Format.fprintf fmt "%s" "year_of_date"
|
||||
| RoundMoney -> Format.fprintf fmt "%s" "money_round"
|
||||
|
||||
let avoid_keywords (s : string) : string =
|
||||
if
|
||||
|
@ -97,6 +97,13 @@ let money_to_string (m : money) : string =
|
||||
Format.asprintf "%.2f" Q.(to_float (of_bigint m / of_int 100))
|
||||
|
||||
let money_to_cents m = m
|
||||
|
||||
let money_round (m : money) : money =
|
||||
let units, cents = Z.div_rem m (Z.of_int 100) in
|
||||
(* If [m] is negative, [cents] will also be negative. *)
|
||||
if Z.(abs cents < of_int 50) then Z.(units * of_int 100)
|
||||
else Z.((units + of_int (sign units)) * of_int 100)
|
||||
|
||||
let decimal_of_string (d : string) : decimal = Q.of_string d
|
||||
let decimal_to_float (d : decimal) : float = Q.to_float d
|
||||
let decimal_of_float (d : float) : decimal = Q.of_float d
|
||||
|
@ -94,6 +94,7 @@ val money_of_cents_integer : integer -> money
|
||||
val money_to_float : money -> float
|
||||
val money_to_string : money -> string
|
||||
val money_to_cents : money -> integer
|
||||
val money_round : money -> money
|
||||
|
||||
(** {2 Decimals} *)
|
||||
|
||||
|
@ -102,6 +102,7 @@ let format_unop (fmt : Format.formatter) (op : Dcalc.Ast.unop Pos.marked) : unit
|
||||
| GetDay -> Format.fprintf fmt "%s" "day_of_month_of_date"
|
||||
| GetMonth -> Format.fprintf fmt "%s" "month_number_of_date"
|
||||
| GetYear -> Format.fprintf fmt "%s" "year_of_date"
|
||||
| RoundMoney -> Format.fprintf fmt "%s" "money_round"
|
||||
|
||||
let avoid_keywords (s : string) : string =
|
||||
if
|
||||
|
@ -297,7 +297,13 @@ type unop = Not | Minus of op_kind
|
||||
nude = true;
|
||||
}]
|
||||
|
||||
type builtin_expression = Cardinal | IntToDec | GetDay | GetMonth | GetYear
|
||||
type builtin_expression =
|
||||
| Cardinal
|
||||
| IntToDec
|
||||
| GetDay
|
||||
| GetMonth
|
||||
| GetYear
|
||||
| RoundMoney
|
||||
[@@deriving
|
||||
visitors { variety = "map"; name = "builtin_expression_map"; nude = true },
|
||||
visitors { variety = "iter"; name = "builtin_expression_iter"; nude = true }]
|
||||
|
@ -911,6 +911,8 @@ let rec translate_expr
|
||||
Bindlib.box (Desugared.Ast.EOp (Dcalc.Ast.Unop Dcalc.Ast.GetMonth), pos)
|
||||
| Builtin GetYear ->
|
||||
Bindlib.box (Desugared.Ast.EOp (Dcalc.Ast.Unop Dcalc.Ast.GetYear), pos)
|
||||
| Builtin RoundMoney ->
|
||||
Bindlib.box (Desugared.Ast.EOp (Dcalc.Ast.Unop Dcalc.Ast.RoundMoney), pos)
|
||||
|
||||
and disambiguate_match_and_build_expression
|
||||
(scope : Scopelang.Ast.ScopeName.t)
|
||||
|
@ -221,6 +221,9 @@ module R = Re.Pcre
|
||||
#ifndef MR_IntToDec
|
||||
#define MR_IntToDec MS_IntToDec
|
||||
#endif
|
||||
#ifndef MR_RoundMoney
|
||||
#define MR_RoundMoney MS_RoundMoney
|
||||
#endif
|
||||
#ifndef MR_GetDay
|
||||
#define MR_GetDay MS_GetDay
|
||||
#endif
|
||||
@ -319,6 +322,7 @@ let lex_builtin (s : string) : Ast.builtin_expression option =
|
||||
| MR_GetDay, eof -> Some GetDay
|
||||
| MR_GetMonth, eof -> Some GetMonth
|
||||
| MR_GetYear, eof -> Some GetYear
|
||||
| MR_RoundMoney, eof -> Some RoundMoney
|
||||
| _ -> None
|
||||
|
||||
(** Regexp matching any digit character.
|
||||
|
@ -99,6 +99,7 @@
|
||||
|
||||
(* Builtins *)
|
||||
|
||||
#define MS_RoundMoney "round_money"
|
||||
#define MS_IntToDec "integer_to_decimal"
|
||||
#define MS_GetDay "get_day"
|
||||
#define MS_GetMonth "get_month"
|
||||
|
@ -120,6 +120,7 @@
|
||||
|
||||
(* Builtins *)
|
||||
|
||||
#define MS_RoundMoney "arrrondi_argent"
|
||||
#define MS_IntToDec "entier_vers_décimal"
|
||||
#define MR_IntToDec "entier_vers_d", 0xE9, "cimal"
|
||||
#define MS_GetDay "accès_jour"
|
||||
|
@ -107,6 +107,8 @@
|
||||
|
||||
(* Builtins *)
|
||||
|
||||
#define MS_RoundMoney "zaokrąglony_pieniądze"
|
||||
#define MR_RoundMoney "zaokr",0x0105,"glony_pieni", 0x0105, "dze"
|
||||
#define MS_IntToDec "calkowita_wers_dziesiętny"
|
||||
#define MR_IntToDec "calkowita_wers_dziesi", 0x0119, "tny"
|
||||
#define MS_GetDay "dostęp_dzień"
|
||||
|
@ -621,7 +621,9 @@ let rec translate_op
|
||||
| GetYear ->
|
||||
failwith
|
||||
"[Z3 encoding] GetYear operator only supported in comparisons with \
|
||||
literal")
|
||||
literal"
|
||||
| RoundMoney ->
|
||||
failwith "[Z3 encoding] RoundMoney operator not implemented yet")
|
||||
|
||||
(** [translate_expr] translate the expression [vc] to its corresponding Z3
|
||||
expression **)
|
||||
|
Binary file not shown.
@ -1090,6 +1090,23 @@ integer_to_decimal of ...
|
||||
```catala
|
||||
entier_vers_décimal de ...
|
||||
```
|
||||
\end{minted}
|
||||
\vspace*{-1.75em}
|
||||
\\
|
||||
Money rounding &
|
||||
\vspace*{-1.75em}
|
||||
\begin{minted}{catala_en}
|
||||
```catala
|
||||
round_money of ...
|
||||
```
|
||||
\end{minted}
|
||||
\vspace*{-1.75em}
|
||||
&
|
||||
\vspace*{-1.75em}
|
||||
\begin{minted}{catala_fr}
|
||||
```catala
|
||||
arrondi_argent de ...
|
||||
```
|
||||
\end{minted}
|
||||
\vspace*{-1.75em}
|
||||
\\
|
||||
|
@ -522,7 +522,7 @@ scope Test3:
|
||||
### Defining exceptions to groups of rules
|
||||
|
||||
Note that the label system also lets you define more complicated exceptions
|
||||
patterns. Sometimes, you want to declare an exception to a groupe of
|
||||
patterns. Sometimes, you want to declare an exception to a group of
|
||||
piecewise definitions. To do that, simply use the same label for all
|
||||
the piecewise definitions.
|
||||
|
||||
@ -727,7 +727,7 @@ in terms of days, months and years. A difference between dates is a duration
|
||||
measured in days, and the addition of a date and a duration yields a new date.
|
||||
Durations measured in days, months or years are not mixable with each other,
|
||||
as months and years do not always have the same number of days. This non-mixability
|
||||
is not captured by the type system of Catala but will yield erros at runtime.
|
||||
is not captured by the type system of Catala but will yield errors at runtime.
|
||||
Date literals are specified using the ISO 8601 standard to avoid confusion between
|
||||
american and european notations. Date operators are prefixed by "@" while
|
||||
duration operators are prefixed by "^".
|
||||
|
@ -9,7 +9,7 @@
|
||||
|
||||
# This file should be in sync with compiler/runtime.{ml, mli} !
|
||||
|
||||
from gmpy2 import log2, mpz, mpq, mpfr, t_divmod # type: ignore
|
||||
from gmpy2 import log2, mpz, mpq, mpfr, t_divmod, sign # type: ignore
|
||||
import datetime
|
||||
import dateutil.relativedelta
|
||||
from typing import NewType, List, Callable, Tuple, Optional, TypeVar, Iterable, Union, Any
|
||||
@ -365,6 +365,14 @@ def money_to_string(m: Money) -> str:
|
||||
def money_to_cents(m: Money) -> Integer:
|
||||
return m.value
|
||||
|
||||
|
||||
def money_round(m: Money) -> Money:
|
||||
res, remainder = t_divmod(m, 100)
|
||||
if remainder < 50:
|
||||
return res * 100
|
||||
else:
|
||||
return (res + sign(res)) * 100
|
||||
|
||||
# --------
|
||||
# Decimals
|
||||
# --------
|
||||
|
Loading…
Reference in New Issue
Block a user