Skip to content

Commit

Permalink
Added function text(mpq, base)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvarrazzo committed Mar 28, 2011
1 parent d14959f commit 8b7cbde
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
37 changes: 37 additions & 0 deletions expected/mpq.out
Expand Up @@ -119,6 +119,43 @@ HINT: base should be between 2 and 62
SELECT mpq('1', 63);
ERROR: invalid base for mpq input: 63
HINT: base should be between 2 and 62
SELECT text('239'::mpq);
239
SELECT text('-239'::mpq);
-239
SELECT text('239/256'::mpq);
239/256
SELECT text('239'::mpq, 16);
ef
SELECT text('239/256'::mpq, 10);
239/256
SELECT text('239/256'::mpq, 16);
ef/100
SELECT text('239/256'::mpq, 0);
ERROR: invalid base for mpq output: 0
HINT: base should be between -36 and -2 or between 2 and 62
SELECT text('239/256'::mpq, 1);
ERROR: invalid base for mpq output: 1
HINT: base should be between -36 and -2 or between 2 and 62
SELECT text('239/256'::mpq, 2);
11101111/100000000
SELECT text('239/256'::mpq, 36);
6n/74
SELECT text('239/256'::mpq, 62);
3r/48
SELECT text('239/256'::mpq, 63);
ERROR: invalid base for mpq output: 63
HINT: base should be between -36 and -2 or between 2 and 62
SELECT text('239/256'::mpq, -1);
ERROR: invalid base for mpq output: -1
HINT: base should be between -36 and -2 or between 2 and 62
SELECT text('239/256'::mpq, -2);
11101111/100000000
SELECT text('239/256'::mpq, -36);
6N/74
SELECT text('239/256'::mpq, -37);
ERROR: invalid base for mpq output: -37
HINT: base should be between -36 and -2 or between 2 and 62
--
-- mpq cast
--
Expand Down
1 change: 1 addition & 0 deletions pgmp.pysql
Expand Up @@ -469,6 +469,7 @@ CREATE TYPE mpq (
!! PYON

func('mpq', 'text int4', 'mpq', cname='pmpq_in_base')
func('text', 'mpq int4', 'cstring', cname='pmpq_out_base')


func('mpq', 'mpz mpz', 'mpq', cname='pmpq_mpz_mpz')
Expand Down
18 changes: 18 additions & 0 deletions sql/mpq.sql
Expand Up @@ -74,6 +74,24 @@ SELECT mpq('1', 1);
SELECT mpq('1', -10);
SELECT mpq('1', 63);

SELECT text('239'::mpq);
SELECT text('-239'::mpq);
SELECT text('239/256'::mpq);
SELECT text('239'::mpq, 16);
SELECT text('239/256'::mpq, 10);
SELECT text('239/256'::mpq, 16);
SELECT text('239/256'::mpq, 0);
SELECT text('239/256'::mpq, 1);
SELECT text('239/256'::mpq, 2);
SELECT text('239/256'::mpq, 36);
SELECT text('239/256'::mpq, 62);
SELECT text('239/256'::mpq, 63);
SELECT text('239/256'::mpq, -1);
SELECT text('239/256'::mpq, -2);
SELECT text('239/256'::mpq, -36);
SELECT text('239/256'::mpq, -37);


--
-- mpq cast
--
Expand Down
24 changes: 24 additions & 0 deletions src/pmpq_io.c
Expand Up @@ -107,6 +107,30 @@ PGMP_PG_FUNCTION(pmpq_out)
PG_RETURN_CSTRING(mpq_get_str(buf, 10, q));
}

PGMP_PG_FUNCTION(pmpq_out_base)
{
const mpq_t q;
int base;
char *buf;

PGMP_GETARG_MPQ(q, 0);
base = PG_GETARG_INT32(1);

if (!((-36 <= base && base <= -2) || (2 <= base && base <= 62)))
{
ereport(ERROR, (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid base for mpq output: %d", base),
errhint("base should be between -36 and -2 or between 2 and 62")));
}

/* Allocate the output buffer manually - see mpmz_out to know why */
buf = palloc(3 /* add sign, slash and null */
+ mpz_sizeinbase(mpq_numref(q), ABS(base))
+ mpz_sizeinbase(mpq_denref(q), ABS(base)));
PG_RETURN_CSTRING(mpq_get_str(buf, base, q));
}


/*
* Cast functions
Expand Down

0 comments on commit 8b7cbde

Please sign in to comment.