integers

In my project, I need to convert a real number into three separate integer values, and print them separately without the pesky ‘.0’ . Is there any way to do this? It’s an angle converted into degrees, minutes and seconds, and of course I am dealing in ‘real’(decimal) numbers.

Thanks

You can say “x to the nearest whole number”–see §15.3 of Writing with Inform.

thanks Matt, I did come across that, but the thing is that it will round up or down, when I only want it to chop off the fraction part. To be more specific–

We have angle x=234. 5678

I would want something like w=INT(x)=234

then y=INT((x + -w) * 60)=INT(.5678 * 60)=INT(34.068)=34

and z=INT(((x + -w) * 60) - y) * 60)=INT(4.08)=4

say “Your angle is [w] degrees [y][’] [z][quotation mark].”

“Your angle is 234 degrees 34’ 4’’.”

I’ve also tried using the ‘floor of’ function, but that only converts to real numbers(and we still get that ‘.0’).

I know that there’s a way to skin this proverbial cat–really all I want to do is print only the part of the number to the left of the decimal point, is this possible? Maybe I can load the number into some string variables, and then print the portion without the ‘.0’ ?

I know that this is just a minor thing, but it’s going into my project.

Thanks for your help

Well, you could make your INT function by subtracting -.5 and then round… that should take you to the next integer down if I have it right? You might have to do different cases for negative and positive numbers. Or you could use “floor of” and then round to the nearest number, maybe? That would be easier to write.

You’re working with a real number but you want to use a plain number in your say phrase (i.e. text output).

The built-in “floor of” phrase will truncate the decimal portion of a real number. The built-in “to the nearest whole number” phrase returns a plain number (i.e. integer) when given a real. I didn’t see a built-in function to get the fractional part of a real, but it’s easy enough to build your own using “floor of”. Put all these together to get what you want, e.g.:

[code]To decide which real number is the fractional portion of (x - a real number):
decide on x minus floor of x.

To decide which real number is the minutes portion of (x - a real number):
decide on the floor of (fractional portion of x) times 60.

To decide which real number is the seconds portion of (x - a real number):
let y be the (fractional portion of x) times 60;
decide on the (fractional portion of y) times 60.

To say interpretation of (x - a real number):
say "[(floor of x) to the nearest whole number] degrees… ";
say "[(minutes portion of x) to the nearest whole number] minutes… ";
say “[(seconds portion of x) to the nearest whole number] seconds”.[/code]

1 Like

You know, I didn’t think of that—thanks to both of you.

Actually, all I really needed to do was say something like–

Xvar is a number that varies. Xvar is 0.
… … …
now Xvar is x to the nearest whole number.(next is the key phrase…)
If xvar > x:
now xvar is xvar - 1;
… … …
say “[xvar] degrees…”

(hitting own head with palm)

Thank you again both and now the subroutine works perfectly!

For the mins and seconds I just had to say something like this–

… … …
now yvar is (x - xvar) * 60;
now xmin is yvar to the nearest whole number;
if xmin > yvar:
now xmin is xmin - 1;
(something similar to the above for the seconds)…
say “[xvar] [xmin] [xsec].”

One thing to beware of, the floor is only the same as truncation when you’re dealing with positive integers. For negative integers, it will round down for you. To have both cases, the best option is probably to convert to text and use a regular expression to capture everything before the separator (.).

It’d be a lot simpler to special-case negative numbers.

Yeah, “if R < 0: …” is going to be a lot less work than messing around with regexps.

Or, here is an I6 implementation:

To decide which number is (R - a real number) truncated to a whole number (this is the trunc-int function): (- REAL_NUMBER_TY_Truncate({R}) -).

Include (-
[ REAL_NUMBER_TY_Truncate val res;
	@ftonumz val res;
	return res;
];
-).
1 Like