Имплементиране функције за конверзију типа бројева
This commit is contained in:
parent
6568f1cc57
commit
da7568e644
2
init.c
2
init.c
|
@ -21,4 +21,6 @@ void init()
|
||||||
addSymbolInternal("*", &multiply);
|
addSymbolInternal("*", &multiply);
|
||||||
addSymbolInternal("/", ÷);
|
addSymbolInternal("/", ÷);
|
||||||
addSymbolInternal("навод", "e);
|
addSymbolInternal("навод", "e);
|
||||||
|
addSymbolInternal("тачно->нетачно", &exactToInexact);
|
||||||
|
addSymbolInternal("нетачно->тачно", &inexactToExact);
|
||||||
}
|
}
|
||||||
|
|
44
internals.c
44
internals.c
|
@ -131,6 +131,50 @@ object divide(object parameters)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
object exactToInexact(object parameters)
|
||||||
|
{
|
||||||
|
object result;
|
||||||
|
|
||||||
|
if (listLength(parameters) != 1)
|
||||||
|
{
|
||||||
|
TYPE(result) = errorObject;
|
||||||
|
ERR(result) = argumentNumberError;
|
||||||
|
}
|
||||||
|
else if (TYPE(CAR(parameters)) != numberObject)
|
||||||
|
{
|
||||||
|
TYPE(result) = errorObject;
|
||||||
|
ERR(result) = typeError;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result = exactToInexactNum(CAR(parameters));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
object inexactToExact(object parameters)
|
||||||
|
{
|
||||||
|
object result;
|
||||||
|
|
||||||
|
if (listLength(parameters) != 1)
|
||||||
|
{
|
||||||
|
TYPE(result) = errorObject;
|
||||||
|
ERR(result) = argumentNumberError;
|
||||||
|
}
|
||||||
|
else if (TYPE(CAR(parameters)) != numberObject)
|
||||||
|
{
|
||||||
|
TYPE(result) = errorObject;
|
||||||
|
ERR(result) = typeError;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result = inexactToExactNum(CAR(parameters));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
object quote(object parameters)
|
object quote(object parameters)
|
||||||
{
|
{
|
||||||
object result;
|
object result;
|
||||||
|
|
|
@ -4,4 +4,6 @@ object add(object parameters);
|
||||||
object subtract(object parameters);
|
object subtract(object parameters);
|
||||||
object multiply(object parameters);
|
object multiply(object parameters);
|
||||||
object divide(object parameters);
|
object divide(object parameters);
|
||||||
|
object exactToInexact(object parameters);
|
||||||
|
object inexactToExact(object parameters);
|
||||||
object quote(object parameters);
|
object quote(object parameters);
|
||||||
|
|
26
util.c
26
util.c
|
@ -1,5 +1,7 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <limits.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
|
@ -96,6 +98,30 @@ object exactToInexactNum(object a)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
object inexactToExactNum(object a)
|
||||||
|
{
|
||||||
|
object result = copyObject(a);
|
||||||
|
|
||||||
|
if (TYPE(result) == numberObject && NUM_TYPE(result) == realNum)
|
||||||
|
{
|
||||||
|
long long int divisor = 1;
|
||||||
|
while (NUM_REAL(result) != floorl(NUM_REAL(result)) &&
|
||||||
|
divisor <= INT_MAX)
|
||||||
|
{
|
||||||
|
NUM_REAL(result) *= 10.0L;
|
||||||
|
divisor *= 10LL;
|
||||||
|
}
|
||||||
|
|
||||||
|
NUM_TYPE(result) = fractionNum;
|
||||||
|
NUM_NUMER(result) = (long long int) floorl(NUM_REAL(result));
|
||||||
|
NUM_DENOM(result) = divisor;
|
||||||
|
|
||||||
|
result = shortenFractionNum(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
long long int gcd(long long int a, long long int b)
|
long long int gcd(long long int a, long long int b)
|
||||||
/* највећи заједнички делилац */
|
/* највећи заједнички делилац */
|
||||||
{
|
{
|
||||||
|
|
1
util.h
1
util.h
|
@ -84,6 +84,7 @@ object copyObject(object input);
|
||||||
object longlongToNumber(long long int input);
|
object longlongToNumber(long long int input);
|
||||||
object shortenFractionNum(object a);
|
object shortenFractionNum(object a);
|
||||||
object exactToInexactNum(object a);
|
object exactToInexactNum(object a);
|
||||||
|
object inexactToExactNum(object a);
|
||||||
object plusNum(object a, object b);
|
object plusNum(object a, object b);
|
||||||
object minusNum(object a);
|
object minusNum(object a);
|
||||||
object timesNum(object a, object b);
|
object timesNum(object a, object b);
|
||||||
|
|
Loading…
Reference in a new issue