Skip to content

Instantly share code, notes, and snippets.

@minoki
minoki / sin-test.c
Created Nov 6, 2020
libmのsinの実装が x=2^n (-1000≤n≤1000)の形の入力に対して「正しく丸められた値」からどのぐらいずれているか検査するやつ
View sin-test.c
// Written by @mod_poppo
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <mpfr.h>
#include <gmp.h>
static void to_normalized_hex(char *buf, mpfr_t a, int mant)
{
// assume a is positive
@minoki
minoki / atcoder-code-prettify.user.js
Last active Sep 5, 2020
AtCoderのシンタックスハイライトをちゃんと言語に応じて動作させるUserScriptです
View atcoder-code-prettify.user.js
// ==UserScript==
// @name AtCoder Code Prettify
// @namespace https://miz-ar.info/
// @include https://atcoder.jp/contests/*/submissions/*
// @version 3
// @grant none
// @run-at document-start
// ==/UserScript==
console.debug("AtCoder Code Prettify is running");
View test-x87-double-rounding.c
#include <stdint.h>
#include <float.h>
#include <stdlib.h>
#include <stdio.h>
#include <fenv.h>
#pragma STDC FENV_ACCESS ON
#if defined(__x86_64__) && defined(__GNUC__)
#define HAS_X87
static void set_x87_prec_24(void)
View float-commutativity.c
#include <stdio.h>
#include <math.h>
#include <inttypes.h>
#if defined(__GNUC__)
#define NOINLINE __attribute__((noinline))
#else
#define NOINLINE
#endif
@minoki
minoki / IntegerToInt.hs
Last active Aug 3, 2020
A code that exhibits "Impossible case alternative"
View IntegerToInt.hs
{-# LANGUAGE MagicHash #-}
module IntegerToInt where
import GHC.Integer.GMP.Internals (Integer (S#))
-- import GHC.Num.Integer (Integer (IS))
import GHC.Exts (Int (I#))
-- Like Data.Bits.toIntegralSized, but optimized for Integer and Int
integerToIntMaybe :: Integer -> Maybe Int
integerToIntMaybe (S# x) = Just (I# x)
-- integerToIntMaybe (IS x) = Just (I# x)
@minoki
minoki / test-fma.c
Last active Aug 12, 2020
A test program to check if your compiler's fma() is implemented correctly
View test-fma.c
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
struct fma_test_case_d
{
double a, b, c, expected;
} static const cases_d[] = {
{0x1p1000, 0x1p1000, -INFINITY, -INFINITY},
{-0x1.4f8ac19291ffap1023, 0x1.39c33c8d39b7p-1025, 0x1.ee11f685e2e12p-1, 0x1.2071b0283f156p-1},
@minoki
minoki / abc169c-counterexamples.c
Created Jun 1, 2020
ABC169 C問題のコーナーケースを生成するやつ
View abc169c-counterexamples.c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <float.h>
#include <inttypes.h>
#if defined(TEST_FLOAT128)
// For _Float128 type, see TS 18661-3
// For _Float128 support on GCC, see https://gcc.gnu.org/onlinedocs/gcc/Floating-Types.html#Floating-Types
View Main.hs
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
import Unboxable
import Mod
import Data.Coerce
-- Defining Unboxable instance is as good as exporting the data constructor!
castUnboxable :: Unboxable a => Rep a -> a
castUnboxable = coerce
@minoki
minoki / CmpNat.hs
Last active Nov 22, 2019
Generating CmpNat constraint at runtime
View CmpNat.hs
#!/usr/bin/env stack
-- stack --resolver lts-14.15 script --package reflection
{-# LANGUAGE DataKinds, RankNTypes, TypeOperators, TypeFamilies, ScopedTypeVariables, TypeApplications #-}
import GHC.TypeNats
import Data.Type.Equality
import Unsafe.Coerce
import Data.Proxy
import Data.Reflection
cmpNat :: (KnownNat a, KnownNat b)
@minoki
minoki / customshow.hs
Created Oct 8, 2019
OVERLAPPINGを使ってShow [Char]の定義を上書きするようなやり方はやばいぞ
View customshow.hs
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE IncoherentInstances #-} -- これがないとエラーになる(この拡張はかなりやばい)
import Data.List
class MyShow a where
myShow :: a -> String
instance MyShow Int where myShow = show
You can’t perform that action at this time.