freebsd-nq/usr.bin/dc/bcode.h
Alan Somers 95639a80ef dc(1): fix input of non-decimal fractional numbers
Inputting fractional non-decimal numbers has never worked correctly in our
OpenBSD-derived dc(1). It truncates the input to a number of decimal places
equal to the number of hexadecimal (or whatever base) places given on the
input. That's unacceptable, because many numbers require more precision to
represent in base 10 than in their original bases.

Fix this bug by using as many decimal places as needed to represent the
input, up to the maximum of the global scale factor.

This has one mildly surprising side effect: the scale of a number entered in
non-decimal mode will no longer necessarily equal the number of hexadecimal
(or whatever base) places given on the input. I think that's an acceptable
behavior change, given that inputting fractional non-decimal numbers never
worked in the first place, and the man page doesn't specify whether trailing
zeros on the input should affect a number's scale.

PR:		206230
Reported by:	nibbana@gmx.us
Reviewed by:	pfg
Differential Revision:	https://reviews.freebsd.org/D13336
2017-12-05 04:22:35 +00:00

105 lines
2.2 KiB
C

/* $FreeBSD$ */
/* $OpenBSD: bcode.h,v 1.7 2012/11/07 11:06:14 otto Exp $ */
/*
* Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <openssl/bn.h>
struct number {
BIGNUM *number;
u_int scale;
};
enum stacktype {
BCODE_NONE,
BCODE_NUMBER,
BCODE_STRING
};
enum bcode_compare {
BCODE_EQUAL,
BCODE_NOT_EQUAL,
BCODE_LESS,
BCODE_NOT_LESS,
BCODE_GREATER,
BCODE_NOT_GREATER
};
struct array;
struct value {
union {
struct number *num;
char *string;
} u;
struct array *array;
enum stacktype type;
};
struct array {
struct value *data;
size_t size;
};
struct stack {
struct value *stack;
ssize_t size;
ssize_t sp;
};
struct source;
struct vtable {
int (*readchar)(struct source *);
void (*unreadchar)(struct source *);
char *(*readline)(struct source *);
void (*free)(struct source *);
};
struct source {
union {
struct {
u_char *buf;
size_t pos;
} string;
FILE *stream;
} u;
struct vtable *vtable;
int lastchar;
};
void init_bmachine(bool);
void reset_bmachine(struct source *);
u_int bmachine_scale(void);
void scale_number(BIGNUM *, int);
void normalize(struct number *, u_int);
void eval(void);
void pn(const char *, const struct number *);
void pbn(const char *, const BIGNUM *);
void negate(struct number *);
void split_number(const struct number *, BIGNUM *, BIGNUM *);
void bmul_number(struct number *, struct number *,
struct number *, u_int scale);
static __inline u_int
max(u_int a, u_int b)
{
return (a > b ? a : b);
}