Gamestudio Links
Zorro Links
Newest Posts
What are you working on?
by Emre
Today at 01:03
New Zorro version 1.83
by claudio
Yesterday at 17:22
Ahead bias when retrain
by jcl
Yesterday at 12:27
Huge array for Deep Learning
by jcl
Yesterday at 12:06
[LBGUI] Memory not released!
by EVO
05/23/18 22:39
http_post undeclared identifier
by Superku
05/23/18 22:10
Pan/Tilt in relation to entity?
by Superku
05/23/18 20:59
AUM Magazine
Latest Screens
Lake scene
Forgotten Tales - Demo version
RPG PARTY
Triton Wing now available on Steam
Who's Online
9 registered (Slin, Quad, Superku, AndrewAMD, 3run, jyd, mk_1, 2 invisible), 464 Guests and 3 Spiders online.
Key: Admin, Global Mod, Mod
Newest Members
Rayharry, AgentShark, rpauletto, tuuPaul, LaserJock2000
17960 Registered Users
Topic Options
Rate This Topic
#472704 - 05/15/18 12:44 [SUB] Fractions
txesmi Offline
Serious User

Registered: 06/13/07
Posts: 1120
Loc: Hiporope and its pain
Hi!
I wrote a little module to work with fractions.

ifractions.h
Code:
#ifndef _IFRACTIONS_H_
#define _IFRACTIONS_H_

#define TEMPFRAC_COUNT   16

typedef struct IFRAC {
	int num;
	int den;
} IFRAC;

IFRAC *ifraction(int _num, int _den); // temporal ifrac

IFRAC *ifracSet(IFRAC *_ifracA, IFRAC *_ifracB);

IFRAC *ifracAdd(IFRAC *_ifracA, IFRAC *_ifracB);
IFRAC *ifracSub(IFRAC *_ifracA, IFRAC *_ifracB);
IFRAC *ifracDiff(IFRAC *_ifracR, IFRAC *_ifracA, IFRAC *_ifracB);
IFRAC *ifracMul(IFRAC *_ifracA, IFRAC *_ifracB);
IFRAC *ifracDiv(IFRAC *_ifracA, IFRAC *_ifracB);

IFRAC *ifracSimplify(IFRAC *_ifrac);
IFRAC *ifracCommonDen(IFRAC *_ifracA, IFRAC *_ifracB);

var ifracToNum(IFRAC *_ifrac);
IFRAC *ifracForNum(IFRAC *_ifrac, var _n);

void ifracPrintf(IFRAC *_ifrac);

#include "ifractions.c"
#endif



ifractions.c
Code:
int _mathGCD(int _a, int _b) {
	for (;;) {
		if (_a == 0)
			return _b;
		_b %= _a;
		if (_b == 0)
			return _a;
		_a %= _b;
	}
}

int _mathLCM(int _a, int _b) {
	int _t = _mathGCD(_a, _b);
	if (!_t)
		return 0;
	return _a / _t * _b;
}

IFRAC _ifracTemp[TEMPFRAC_COUNT];
IFRAC *_ifracZero = {num = 0; den = 1;}


IFRAC *ifraction(int _num, int _den) {
	static int _i = 0;
	IFRAC *_ifrac = _ifracTemp + _i;
	_i = (_i + 1) % TEMPFRAC_COUNT;
	memcpy(_ifrac, &_num, sizeof(IFRAC));
	return ifracSimplify(_ifrac);
}

IFRAC *ifracSet(IFRAC *_ifracA, IFRAC *_ifracB) {
	memcpy(_ifracA, _ifracB, sizeof(IFRAC));
	return ifracSimplify(_ifracA);
}

IFRAC *ifracSimplify(IFRAC *_ifrac) {
	int _gcd = _mathGCD(abs(_ifrac->num), abs(_ifrac->den));
	if (_gcd) {
		_ifrac->num /= _gcd;
		_ifrac->den /= _gcd;
	}
	if ((_ifrac->num == 0) || (_ifrac->den == 0)) {
		memcpy(_ifrac, _ifracZero, sizeof(IFRAC));
	} else if (_ifrac->den < 0) {
		_ifrac->num *= -1;
		_ifrac->den *= -1;
	}
	return _ifrac;
}

IFRAC *ifracCommonDen(IFRAC *_ifracA, IFRAC *_ifracB) {
	int _lcm = _mathLCM(_ifracA->den, _ifracB->den);
	_ifracA->num *= _lcm / _ifracA->den;
	_ifracA->den = _lcm;
	_ifracB->num *= _lcm / _ifracB->den;
	_ifracB->den = _lcm;
	return _ifracA;
}

IFRAC *ifracAdd(IFRAC *_ifracA, IFRAC *_ifracB) {
	IFRAC *_ifracT = ifraction(_ifracB->num, _ifracB->den);
	ifracCommonDen(_ifracA, _ifracT);
	_ifracA->num += _ifracT->num;
	return ifracSimplify(_ifracA);
}

IFRAC *ifracSub(IFRAC *_ifracA, IFRAC *_ifracB) {
	IFRAC *_ifracT = ifraction(_ifracB->num, _ifracB->den);
	ifracCommonDen(_ifracA, _ifracT);
	_ifracA->num -= _ifracT->num;
	return ifracSimplify(_ifracA);
}

IFRAC *ifracDiff(IFRAC *_ifracR, IFRAC *_ifracA, IFRAC *_ifracB) {
	ifracSet(_ifracR, _ifracA);
	return ifracSub(_ifracR, _ifracB);
}

IFRAC *ifracMul(IFRAC *_ifracA, IFRAC *_ifracB) {
	_ifracA->num *= _ifracB->num;
	_ifracA->den *= _ifracB->den;
	return ifracSimplify(_ifracA);
}

IFRAC *ifracDiv(IFRAC *_ifracA, IFRAC *_ifracB) {
	_ifracA->num *= _ifracB->den;
	_ifracA->den *= _ifracB->num;
	return ifracSimplify(_ifracA);
}

var ifracToNum(IFRAC *_ifrac) {
	return (var)_ifrac->num / _ifrac->den;
}

IFRAC *ifracForNum(IFRAC *_ifrac, var _n) {
	_ifrac->num = _n << 10;
	_ifrac->den = 1024;
	return ifracSimplify(_ifrac);
}

void ifracPrintf(IFRAC *_ifrac) {
	printf("%d / %d", _ifrac->num, _ifrac->den);
}



Maybe a bit overheaded of simplification.

Enjoy!


Edited by txesmi (05/15/18 13:40)
Edit Reason: error!! fixed

Top
#472705 - 05/15/18 12:56 Re: [SUB] Fractions [Re: txesmi]
3run Online
Senior Expert

Registered: 05/05/09
Posts: 5020
Loc: Caucasus
Thank you man! laugh
_________________________
Looking for free stuff?? Take a look here: http://badcom.at.ua

Top
#472706 - 05/15/18 13:41 Re: [SUB] Fractions [Re: 3run]
txesmi Offline
Serious User

Registered: 06/13/07
Posts: 1120
Loc: Hiporope and its pain
there was a bug...

You are welcome wink

Top


Moderator:  HeelX, Lukas, Rei_Ayanami, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de