Skip to content

Instantly share code, notes, and snippets.

View tyabu12's full-sized avatar

Tomohito YABU tyabu12

  • Tokyo, Japan
View GitHub Profile
template<typename T, std::size_t N>
inline std::size_t array_length(const T(&)[N]) { return N; }
#include <sstream>
template<typename T>
inline T from_s(const std::string& s) { T v; std::istringstream i(s); i >> v; return v; }
template<typename T>
inline std::string to_s(const T& x) { std::ostringstream o; o << x; return o.str(); }
#include <vector>
// if parent[x] < 0, x is root and |parent[x]| is a number of the set
// if parent[x] >= 0, x is child
class UnionFind {
private:
std::vector<int> parent;
bool double_cnt;
public:
void replace(std::string& s, const std::string& from, const std::string& to)
{
std::size_t p1 = 0, p2;
while ((p2 = s.find(from, p1)) != s.npos) {
s.replace(p2, from.size(), to);
p1 = p2;
}
}
void split(std::vector<std::string>& dst, const std::string& src,
const std::string& delimiter, bool ignore_empty = false)
{
std::size_t p1 = 0;
do {
std::size_t p2 = src.find(delimiter, p1);
if (p2 == src.npos)
p2 = src.size();
if (p2 != p1 || !ignore_empty)
dst.push_back(src.substr(p1, p2-p1));
type round_mode =
| NearestTiesToEven
| ToZero
| Up
| Down
(* | NearestTiesToAway *)
external add: round_mode -> float -> float -> float = "addc"
#include <stdio.h>
#include <math.h>
#include <fenv.h>
#include <assert.h>
#include <caml/mlvalues.h>
#include <caml/alloc.h>
static void change_round_mode(int mode) {
switch (mode) {
case NearestTiesToEven:
let print x y z =
let print_line mode mode_name =
Printf.printf " round %s:\n x + y = %.18e\n x + z = %.18e\n"
mode_name (Round.add mode x y) (Round.add mode x z) in
Printf.printf "x = %e, y = %e, z = %e\n" x y z;
print_line Round.NearestTiesToEven "NearestTiesToEven";
print_line Round.ToZero "ToZero";
print_line Round.Up "Up";
print_line Round.Down "Down";
Printf.printf "\n"
x = 1.000000e+00, y = 1.000000e-40, z = -1.000000e-40
round NearestTiesToEven:
x + y = 1.000000000000000000e+00
x + z = 1.000000000000000000e+00
round ToZero:
x + y = 1.000000000000000000e+00
x + z = 9.999999999999998890e-01
round Up:
x + y = 1.000000000000000222e+00
x + z = 1.000000000000000000e+00
CAMLC = ocamlc
CAMLOPT = ocamlopt
CAMLMKLIB = ocamlmklib
EXEC = test testopt
all: round.cma round.cmxa $(EXEC)
round.o: round.c
$(CAMLC) -c $<