kemuniku / cplib

Creative Commons Zero v1.0 Universal
4 stars 0 forks source link

int128型 #137

Open kemuniku opened 7 months ago

kemuniku commented 7 months ago

ほしい

seekworser commented 7 months ago

https://github.com/status-im/nim-stint

kemuniku commented 3 months ago
#https://kenkoooo.hatenablog.com/entry/2016/11/30/163533より引用
{.emit:"#include <bits/stdc++.h>".}
{.emit:"""
using namespace std;
std::ostream &operator<<(std::ostream &dest, __int128_t value) {
  std::ostream::sentry s(dest);
  if (s) {
    __uint128_t tmp = value < 0 ? -value : value;
    char buffer[128];
    char *d = std::end(buffer);
    do {
      --d;
      *d = "0123456789"[tmp % 10];
      tmp /= 10;
    } while (tmp != 0);
    if (value < 0) {
      --d;
      *d = '-';
    }
    int len = std::end(buffer) - d;
    if (dest.rdbuf()->sputn(d, len) != len) {
      dest.setstate(std::ios_base::badbit);
    }
  }
  return dest;
}

__int128 parseint128(const std::string &s) {
  __int128 ret = 0;
  for (int i = 0; i < s.length(); i++)
    if ('0' <= s[i] && s[i] <= '9')
      ret = 10 * ret + s[i] - '0';
  return ret;
}
""".}
type int128 {.importc: "__int128",nodecl.} = object
proc `+`(a,b:int128):int128{.importcpp: "(#)+(#)", nodecl.}
proc `*`(a,b:int128):int128{.importcpp: "(#)*(#)", nodecl.}
proc put(a:int128){.importcpp: "std::cout << (#) << endl;", nodecl.}
proc parse(a:cstring):int128{.importcpp: "parseint128((#))".}
include cplib/tmpl/sheep
var s = cstring("1000000000000000000")
var a = parse(s)
var b = parse(s)
var c = a*b
put(c)

こんな感じで実装はできそう。

kemuniku commented 3 months ago

なんか通らん

kemuniku commented 3 months ago

冷静になると、parseに負が想定されていない

kemuniku commented 3 months ago

ACは取れそう。

kemuniku commented 3 months ago

https://zenn.dev/mizar/articles/fc87d667153080