feat: add Float.nearly_eq

This commit is contained in:
Shunsuke Shibayama 2023-11-07 01:57:47 +09:00
parent b87c075ffa
commit ebf803dab0
4 changed files with 36 additions and 8 deletions

View file

@ -134,28 +134,36 @@ impl Context {
switch_lang!(
"japanese" => {
hint.push_str("Floatは等価関係が定義されていません。");
hint.push_str_with_color_and_attr("l == R", ERR, ATTR);
hint.push_str_with_color_and_attr("l == r", ERR, ATTR);
hint.push_str("ではなく、");
hint.push_str_with_color_and_attr("l - r <= Float.EPSILON", HINT, ATTR);
hint.push_str("あるいは");
hint.push_str_with_color_and_attr("l.nearly_eq(r)", HINT, ATTR);
hint.push_str("を使用してください");
},
"simplified_chinese" => {
hint.push_str("Float没有定义等价关系。你应该使用");
hint.push_str_with_color_and_attr("l == R", ERR, ATTR);
hint.push_str("而不是");
hint.push_str_with_color_and_attr("l - r <= Float.EPSILON", HINT, ATTR);
hint.push_str("或者");
hint.push_str_with_color_and_attr("l.nearly_eq(r)", HINT, ATTR);
hint.push_str("而不是");
hint.push_str_with_color_and_attr("l == r", ERR, ATTR);
},
"traditional_chinese" => {
hint.push_str("Float沒有定義等價關係。你應該使用");
hint.push_str_with_color_and_attr("l == R", ERR, ATTR);
hint.push_str(" instead of ");
hint.push_str_with_color_and_attr("l - r <= Float.EPSILON", HINT, ATTR);
hint.push_str("或者");
hint.push_str_with_color_and_attr("l.nearly_eq(r)", HINT, ATTR);
hint.push_str("而不是");
hint.push_str_with_color_and_attr("l == r", ERR, ATTR);
},
"english" => {
hint.push_str("Float has no equivalence relation defined. you should use ");
hint.push_str_with_color_and_attr("l == R", ERR, ATTR);
hint.push_str(" instead of ");
hint.push_str("Float has no equivalence relation defined. You should use ");
hint.push_str_with_color_and_attr("l - r <= Float.EPSILON", HINT, ATTR);
hint.push_str(" or ");
hint.push_str_with_color_and_attr("l.nearly_eq(r)", HINT, ATTR);
hint.push_str(" instead of ");
hint.push_str_with_color_and_attr("l == r", ERR, ATTR);
},
);
Some(hint.to_string())

View file

@ -148,6 +148,19 @@ impl Context {
float.register_py_builtin(OP_GE, fn1_met(Float, Float, Bool), Some(OP_GE), 0);
float.register_py_builtin(OP_LT, fn1_met(Float, Float, Bool), Some(OP_LT), 0);
float.register_py_builtin(OP_LE, fn1_met(Float, Float, Bool), Some(OP_LE), 0);
let t_nearly_eq = fn_met(
Float,
vec![kw(KW_OTHER, Float)],
None,
vec![kw(KW_EPSILON, Float)],
Bool,
);
float.register_builtin_erg_impl(
FUNC_NEARLY_EQ,
t_nearly_eq,
Immutable,
Visibility::BUILTIN_PUBLIC,
);
let t_call = func1(Obj, Float);
float.register_builtin_erg_impl(
FUNDAMENTAL_CALL,

View file

@ -410,6 +410,7 @@ const FUNC_HASATTR: &str = "hasattr";
const FUNC_GETATTR: &str = "getattr";
const FUNC_SETATTR: &str = "setattr";
const FUNC_DELATTR: &str = "delattr";
const FUNC_NEARLY_EQ: &str = "nearly_eq";
const OP_EQ: &str = "__eq__";
const OP_HASH: &str = "__hash__";
@ -575,6 +576,7 @@ const KW_WHENCE: &str = "whence";
const KW_CHARS: &str = "chars";
const KW_OTHER: &str = "other";
const KW_CONFLICT_RESOLVER: &str = "conflict_resolver";
const KW_EPSILON: &str = "epsilon";
pub fn builtins_path() -> PathBuf {
erg_pystd_path().join("builtins.d.er")