From 165ccc9ee2838c54d4347d130453c7193bd3d596 Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Thu, 1 Dec 1994 05:00:46 +0000 Subject: [PATCH] Initial revision --- math.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 math.c diff --git a/math.c b/math.c new file mode 100644 index 0000000..a0b8437 --- /dev/null +++ b/math.c @@ -0,0 +1,96 @@ +/* %W% %G% %U% - (c) Copyright 1987, 1988 Chuck Simmons */ + +/* + * Copyright (C) 1987, 1988 Chuck Simmons + * + * See the file COPYING, distributed with empire, for restriction + * and warranty information. + */ + +/* +math.c -- various mathematical routines. + +This file contains routines used to create random integers. The +initialization routine 'rndini' should be called at program startup. +The flavors of random integers that can be generated are: + + irand(n) -- returns a random integer in the range 0..n-1 + rndint(a,b) -- returns a random integer in the range a..b + +Other routines include: + + dist (a, b) -- returns the straight-line distance between two locations. +*/ + +#include "empire.h" +#include "extern.h" + +void rndini() +{ + srand((unsigned)(time(0) & 0xFFFF)); +} + +long irand(high) +long high; +{ + if (high < 2) { + return (0); + } + return (rand() % high); +} + +int rndint(minp, maxp) +int minp, maxp; +{ + int size; + + size = maxp - minp + 1; + return ((rand() % size) + minp); +} + +/* +Return the distance between two locations. This is simply +the max of the absolute differnce between the x and y coordinates. +*/ + +#define MIN(a,b) ((a)<(b) ? (a) : (b)) +#define MAX(a,b) ((a)>(b) ? (a) : (b)) +#define ABS(a) ((a) < 0 ? -(a) : (a)) + +dist (a, b) +long a, b; +{ + int ax, ay, bx, by; + + ax = loc_row (a); + ay = loc_col (a); + bx = loc_row (b); + by = loc_col (b); + + return (MAX (ABS (ax-bx), ABS (ay-by))); +} + +/* +Find the square root of an integer. We actually return the floor +of the square root using Newton's method. +*/ + +int sqrt (n) +int n; +{ + int guess; + + ASSERT (n >= 0); /* can't take sqrt of negative number */ + + if (n <= 1) return (n); /* do easy cases and avoid div by zero */ + + guess = 2; /* gotta start somewhere */ + guess = (guess + n/guess) / 2; + guess = (guess + n/guess) / 2; + guess = (guess + n/guess) / 2; + guess = (guess + n/guess) / 2; + guess = (guess + n/guess) / 2; + + if (guess * guess > n) guess -= 1; /* take floor */ + return (guess); +} -- 2.43.0