

CREATE TABLE plausible_rscale_adjustments
(
    var1ndigits integer,
    var2ndigits integer,
    var1dscale integer,
    var2dscale integer,
    rscale_adjustment integer
);

\copy plausible_rscale_adjustments from plausible_rscale_adjustments.csv with csv;


CREATE TABLE test_numeric_mul_rscale
(
    var1 numeric,
    var2 numeric,
    rscale_adjustment integer,
    expected numeric
);

CREATE TABLE test_numeric_power
(
    var1 numeric,
    var2 numeric,
    expected numeric
);

CREATE TABLE test_numeric_exp
(
    var numeric,
    expected numeric
);

SELECT setseed(0);

INSERT INTO test_numeric_mul_rscale
    (var1, var2, rscale_adjustment, expected)
SELECT
    var1,
    var2,
    rscale_adjustment,
    numeric_mul_rscale(var1, var2, rscale_adjustment)
FROM
(
    SELECT
        rscale_adjustment,
        round(random(
            format('1%s',repeat('0',(var1ndigits-1)*4-1))::numeric,
            format('%s',repeat('9',var1ndigits*4))::numeric
        ) / 10::numeric^var1dscale, var1dscale) AS var1,
        round(random(
            format('1%s',repeat('0',(var2ndigits-1)*4-1))::numeric,
            format('%s',repeat('9',var2ndigits*4))::numeric
        ) / 10::numeric^var2dscale, var2dscale) AS var2
    FROM
        plausible_rscale_adjustments,
        generate_series(1, 10000)
) AS q;

INSERT INTO test_numeric_power
    (var1, var2, expected)
SELECT
    var1,
    var2,
    numeric_power(var1, var2)
FROM
(
    SELECT
        random(-99.999,99.999) AS var1,
        random(0,100) AS var2
    FROM
        generate_series(1, 1000000)
) AS q;

INSERT INTO test_numeric_power
    (var1, var2, expected)
SELECT
    var1,
    var2,
    numeric_power(var1, var2)
FROM
(
    SELECT
        random(0.000,99.999) AS var1,
        random(0.0000000000000000,100.0000000000000000) AS var2
    FROM
        generate_series(1, 100000)
) AS q;

INSERT INTO test_numeric_exp
    (var, expected)
SELECT
    var,
    numeric_exp(var)
FROM
(
    SELECT
        random(-99.999,99.999) AS var
    FROM
        generate_series(1, 1000000)
) AS q;

\copy test_numeric_mul_rscale to test_numeric_mul_rscale.dump;
\copy test_numeric_power to test_numeric_power.dump;
\copy test_numeric_exp to test_numeric_exp.dump;
