blueloveTH 2 anni fa
parent
commit
1444c4b380
3 ha cambiato i file con 11 aggiunte e 1 eliminazioni
  1. 4 0
      docs/modules/math.md
  2. 1 1
      docs/modules/requests.md
  3. 6 0
      src/pocketpy.h

+ 4 - 0
docs/modules/math.md

@@ -58,3 +58,7 @@ Return the smallest integer value greater than or equal to `x`.
 ### `math.sqrt(x)`
 
 Return the square root of `x`.
+
+### `math.gcd(a, b)`
+
+Return the greatest common divisor of `a` and `b`.

+ 1 - 1
docs/modules/requests.md

@@ -4,7 +4,7 @@ label: requests
 ---
 
 !!!
-This module is experimental. To enable it, download `httplib.h` from https://github.com/yhirose/cpp-httplib and place it in the same directory as `pocketpy.h`.
+This module is experimental. To enable it, download `httplib.h` from [here](https://github.com/yhirose/cpp-httplib) and place it in the same directory as `pocketpy.h`.
 
 SSL is not supported.
 !!!

+ 6 - 0
src/pocketpy.h

@@ -775,6 +775,12 @@ inline void add_module_math(VM* vm){
     vm->bind_func<1>(mod, "floor", CPP_LAMBDA(VAR((i64)std::floor(vm->num_to_float(args[0])))));
     vm->bind_func<1>(mod, "ceil", CPP_LAMBDA(VAR((i64)std::ceil(vm->num_to_float(args[0])))));
     vm->bind_func<1>(mod, "sqrt", CPP_LAMBDA(VAR(std::sqrt(vm->num_to_float(args[0])))));
+    vm->bind_func<2>(mod, "gcd", [](VM* vm, ArgsView args) {
+        i64 a = CAST(i64, args[0]);
+        i64 b = CAST(i64, args[1]);
+        a = std::gcd(a, b);
+        return VAR(a);
+    });
 }
 
 inline void add_module_dis(VM* vm){