Skip to content

Commit

Permalink
Raise an error in random(QQ) if height is nonpositive
Browse files Browse the repository at this point in the history
  • Loading branch information
d-torrance committed Sep 22, 2024
1 parent 82aabfe commit 7fabcd3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
3 changes: 1 addition & 2 deletions M2/Macaulay2/d/interface.dd
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ export rawFareyApproximation(e:Expr):Expr := (
setupfun("rawFareyApproximation", rawFareyApproximation);
export rawRandomQQ(e:Expr):Expr := (
when e
is Nothing do toExpr(Ccode(QQ, "rawRandomQQ(", "0)"))
is ht:ZZcell do toExpr(Ccode(QQ, "rawRandomQQ(", ht.v, ")"))
is ht:ZZcell do toExpr(Ccode(QQorNull, "rawRandomQQ(", ht.v, ")"))
else WrongArgZZ());
setupfun("rawRandomQQ",rawRandomQQ);
export rawRandomRRUniform(e:Expr):Expr := (
Expand Down
15 changes: 14 additions & 1 deletion M2/Macaulay2/e/interface/random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include "interface/random.h"
#include "interface/gmp-util.h"

#include "exceptions.hpp"
#include "error.h"

#define INITIALMAXINT 10

#define IA 16807
Expand Down Expand Up @@ -152,6 +155,9 @@ void rawSetRandomQQ(mpq_ptr result, gmp_ZZ height)
mpfr_t x;

if (height == nullptr) height = maxHeight;
if (mpz_cmp_si(height, 0) <= 0)
throw exc::engine_error("expected a positive height");

mpfr_init2(x, gmp_defaultPrecision);
mpfr_urandomb(x, state);
mpfr_mul_z(x, x, height, MPFR_RNDN);
Expand All @@ -165,7 +171,14 @@ gmp_QQ rawRandomQQ(gmp_ZZ height)
{
mpq_ptr result = getmemstructtype(mpq_ptr);
mpq_init(result);
rawSetRandomQQ(result, height);

try {
rawSetRandomQQ(result, height);
} catch (const exc::engine_error& e) {
ERROR(e.what());
return nullptr;
}

return moveTo_gmpQQ(result);
}

Expand Down
14 changes: 12 additions & 2 deletions M2/Macaulay2/e/matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,12 @@ Matrix *Matrix::random(
int32_t u = rawRandomInt((int32_t)10000);
if (u > threshold) continue;
}
mat.set_entry(j, i, R->random());
try {
mat.set_entry(j, i, R->random());
} catch (const exc::engine_error& e) {
ERROR(e.what());
return nullptr;
}
}
}
else if (special_type == 1)
Expand All @@ -760,7 +765,12 @@ Matrix *Matrix::random(
int32_t u = rawRandomInt((int32_t)10000);
if (u > threshold) continue;
}
mat.set_entry(j, i, R->random());
try {
mat.set_entry(j, i, R->random());
} catch (const exc::engine_error& e) {
ERROR(e.what());
return nullptr;
}
}
}
}
Expand Down

0 comments on commit 7fabcd3

Please sign in to comment.