The extension needs some testing. If you would like to use it, it is
available on the following link:
http://prolixic.inform7.googlepages.com/FloatingPointMaths.i7x
All comments and suggestions for improvements welcome.
Some words of warning:
I am a lawyer and priest by training, not a mathematician or computer
programmer so some of the functions may need refining. Most of the
functions are calculated using brute force and ignorance.
Error checking routines have not yet been added so things like
division by 0, overflows, underflows and square roots of negative
numbers have not been trapped.
The extension requires the use of Glulx.
It is set up for four decimal places. This can be changed but it
requires editing the extension. Only numbers in the range
-214748.3648 to 214748.3647 are supported.
Documentation has to be added.
Mike
> I am a lawyer and priest by training,
O_o o_O O_O
what ?
Best regards from Italy,
Dott. Piergiorgio.
Technically, these are fixed point numbers, since they all have the
same number of decimal places.
Floating point combines a fixed point number with an exponent, so each
number can have a different number of decimal places. You could use
I7's units to make a floating point system too, as long as you don't
mind writing the numbers in scientific notation.
vw
Fair comment! Subject line changed accordingly.
>> Technically, these are fixed point numbers, since they all have the
>> same number of decimal places.
>>
>> vw
>
> Fair comment! Subject line changed accordingly.
This is pretty cool, Mike. I especially like your factorial function. I
know it's a standard definition, but I didn't know I7 funcs could do
that kind of self - referential recursion. Two quick things though,
you're missing a line in the tangent func:
To decide what fp_number is the tangent of (N - a fp_number):
let temp1 be the sine of N;
let temp2 be the cosine of N;
let result be temp1 fp_divided by temp2;
decide on result. [<-- missing line]
Also, I would recommend leaving out the paragraph break in your "To say
(N - a fp_number) as a number:" phrase. For the extension user, adding a
paragraph break is trivial, removing an existing one without rewriting
the phrase is impossible.
Skinny Mike
Thanks for this Mike. The changes have been made. I will upload a
new version to the test site later today with some documentation and
additional functions added.
I take no credit for discovering that I7 can use recursive functions.
Emily posted on this subject many moons ago with an example using a
factorial function.
Test version updated. Now available on the following link (to reflect
the change of name):
http://prolixic.inform7.googlepages.com/FixedPointMaths.i7x
Mike
Hi Mike, I toyed around with the new version a tiny bit so far.
Is it really necessary to attached "fp_" onto everything? It seems
very non-Inform. For example, declare the unit as:
A real number is a kind of value. -999999.9999 specifies a real
number with parts whole and fractional.
It reads better: "the fractional part of X".
Similarly for "fp_divided by" and the like. I -think- you can get
away with just declaring it "divided by" and Inform will do the right
thing based on type (Unit vs. Number). But if not, at least use
something like "exactly/precisely divided by" or some such.
I like "as an integer". I use the same wording for typecasts. :)
"the reverse of X": Shouldn't that be "to negate", or "the
negation"?
I think your "To decide which number is the greater part of (N - a
number):" function is just doing the same thing as the built-in "the
whole part of N"? Similar for "lesser part of" and "fractional part
of N". Although I understand defining synonyms for forgetful
authors, the fact that you use this function call (and associated
overhead) instead of the built-in method makes me wonder, especially
as it's a call to Divide. Or have I misunderstood the purpose for
it? I admit the necessity of the HalfDivisor business I'm not clear
on.
That's about all I had. You put a lot of work into that, I can tell!
-R
I like that and will incorporate it into the released version.
> Similarly for "fp_divided by" and the like. I -think- you can get
> away with just declaring it "divided by" and Inform will do the right
> thing based on type (Unit vs. Number). But if not, at least use
> something like "exactly/precisely divided by" or some such.
>
I did try this with the original version but it sometimes threw up
errors where Inform 7 was expecting integers. I think that this may
be because the extension allows you to include a real number and an
integer in a phrase such as "int fp_plus real" and this threw Inform 7
where I was using "int plus real". Again this could be changed to
real plus, real minus, real times, real divided by. Would this read
better?
> I like "as an integer". I use the same wording for typecasts. :)
>
> "the reverse of X": Shouldn't that be "to negate", or "the
> negation"?
>
I did think of something like this but it tends to give the impression
that the result is always a negative. Would "X with its sign
reversed" be acceptable?
> I think your "To decide which number is the greater part of (N - a
> number):" function is just doing the same thing as the built-in "the
> whole part of N"? Similar for "lesser part of" and "fractional part
> of N". Although I understand defining synonyms for forgetful
> authors, the fact that you use this function call (and associated
> overhead) instead of the built-in method makes me wonder, especially
> as it's a call to Divide. Or have I misunderstood the purpose for
> it? I admit the necessity of the HalfDivisor business I'm not clear
> on.
>
The greater and lesser parts functions are internal housekeeping
ones. For example, a real number is converted to an integer 1234.5678
becomes 12345678 before addition. If the result is an integer
1234567890, the greater part of X returns 123456 and the lesser part
of X returns 7890. These are used to build the answer as the real
number 123456.7890.
Half Divisor is used to determine, where appropriate whether the
fourth decimal place of a number needs to be rounded up.
> That's about all I had. You put a lot of work into that, I can tell!
>
Thanks for your feedback.
> -R
Well, if we've no choice, then it's better than underscores I guess.
OTOH, could you replace the built-in "plus" so that only a single To
phrase deals with all cases?
> > "the reverse of X": Shouldn't that be "to negate", or "the
> I did think of something like this but it tends to give the impression
> that the result is always a negative. Would "X with its sign
> reversed" be acceptable?
Er, I'll punt to the author by saying allow either: the/a/an/--
reverse/negation of (N - a number)
> The greater and lesser parts functions are internal housekeeping
> ones. For example, a real number is converted to an integer 1234.5678
> becomes 12345678 before addition. If the result is an integer
> 1234567890, the greater part of X returns 123456 and the lesser part
> of X returns 7890. These are used to build the answer as the real
> number 123456.7890.
Ah, that makes sense. Sorry to bring it up.
-R