f

#### Integers in BASIC

```In BASIC the result of PRINT INT(1.234) <> 1.234 is -1. Anyone explain
why?

Richard

```
 0
Richard
12/17/2016 3:10:26 PM
comp.sys.acorn.programmer 2499 articles. 0 followers.

18 Replies
397 Views

Similar Articles

[PageSpeed] 14

```In article <55efeec45bbasura@invalid.addr.uk>,
Richard Ashbery <basura@invalid.addr.uk> wrote:
> In BASIC the result of PRINT INT(1.234) <> 1.234 is -1.
> Anyone explain why?

What do you expect INT(1.234) to be?
The Integer part of 1.234 is 1 ... which is indeed not equal to 1.234.
So the result is TRUE ... which is -1
And the result is PRINTed so shows -1.

I fear you have a misconception somewhere?

--
Martin Avison
Note that unfortunately this email address will become invalid
without notice if (when) any spam is received.
```
 0
Martin
12/17/2016 3:45:13 PM
```Richard Ashbery, on 17 Dec, wrote:

> In BASIC the result of PRINT INT(1.234) <> 1.234 is -1. Anyone explain
> why?

P. INT(1.234)<>1.234
-1
P. INT(1.234)=1.234
0

P. TRUE
-1
P.FALSE
0

--
David Pitt
```
 0
David
12/17/2016 3:49:58 PM
```In article <55eff1f3c1News03@avisoft.f9.co.uk>, Martin
<News03@avisoft.f9.co.uk> wrote:
> In article <55efeec45bbasura@invalid.addr.uk>, Richard Ashbery
>    <basura@invalid.addr.uk> wrote:
> > In BASIC the result of PRINT INT(1.234) <> 1.234 is -1. Anyone
> > explain why?

> What do you expect INT(1.234) to be? The Integer part of 1.234 is 1
> ... which is indeed not equal to 1.234. So the result is TRUE ...
> which is -1 And the result is PRINTed so shows -1.

> I fear you have a misconception somewhere?

I think you are being polite, Martin

My problem came when I did the calculation using BBC BASIC for Windows
and got different results when they should be the same. The full
statement was...

PRINT INT(1.234) - (INT(1.234) <> 1.234)

It was my error - I had forgotten to put brackets around the
INT(1.234) <> 1.234 part on the RISC OS machine which obviously gives
a different result. The correct result is 2 not -1.

Sorry for wasting your time.

Richard

```
 0
Richard
12/17/2016 4:31:55 PM
```In article <55eff63a0fbasura@invalid.addr.uk>,
Richard Ashbery <basura@invalid.addr.uk> wrote:
> In article <55eff1f3c1News03@avisoft.f9.co.uk>, Martin
> <News03@avisoft.f9.co.uk> wrote:
> > In article <55efeec45bbasura@invalid.addr.uk>, Richard Ashbery
> >    <basura@invalid.addr.uk> wrote:
> > > In BASIC the result of PRINT INT(1.234) <> 1.234 is -1. Anyone
> > > explain why?

> > What do you expect INT(1.234) to be? The Integer part of 1.234 is 1
> > ... which is indeed not equal to 1.234. So the result is TRUE ...
> > which is -1 And the result is PRINTed so shows -1.

> My problem came when I did the calculation using BBC BASIC for Windows
> and got different results when they should be the same. The full
> statement was...

> PRINT INT(1.234) - (INT(1.234) <> 1.234)

Ahah! So it was only half of the question.

> It was my error - I had forgotten to put brackets around the
> INT(1.234) <> 1.234 part on the RISC OS machine which obviously gives
> a different result. The correct result is 2 not -1.

So are you really asking: how do I round up a real number to the next
whole integer? Or something more obscure.

If rounding up, a simpler way might be
PRINT INT(1.234 + 0.9999)
(as long as the number of 9's exceeds the digits after the decimal point)

A common requirement is to 'half adjust' ie round to the nearest integer,
which is usually done by
PRINT INT(1.234 + 0.5)

Martin

--
Martin Avison
Note that unfortunately this email address will become invalid
without notice if (when) any spam is received.
```
 0
Martin
12/18/2016 9:57:32 AM
```In article <55f055f4edNews03@avisoft.f9.co.uk>, Martin
<News03@avisoft.f9.co.uk> wrote:
> In article <55eff63a0fbasura@invalid.addr.uk>, Richard Ashbery
>    <basura@invalid.addr.uk> wrote:
> > In article <55eff1f3c1News03@avisoft.f9.co.uk>, Martin
> > <News03@avisoft.f9.co.uk> wrote:
> > > In article <55efeec45bbasura@invalid.addr.uk>, Richard Ashbery
> > >    <basura@invalid.addr.uk> wrote:
> > > > In BASIC the result of PRINT INT(1.234) <> 1.234 is -1.
> > > > Anyone explain why?

> > > What do you expect INT(1.234) to be? The Integer part of 1.234
> > > is 1 ... which is indeed not equal to 1.234. So the result is
> > > TRUE ... which is -1 And the result is PRINTed so shows -1.

> > My problem came when I did the calculation using BBC BASIC for
> > Windows and got different results when they should be the same.
> > The full statement was...

> > PRINT INT(1.234) - (INT(1.234) <> 1.234)

> Ahah! So it was only half of the question.

> > It was my error - I had forgotten to put brackets around the
> > INT(1.234) <> 1.234 part on the RISC OS machine which obviously
> > gives a different result. The correct result is 2 not -1.

> So are you really asking: how do I round up a real number to the
> next whole integer? Or something more obscure.

Rounding up yes - nothing more obscure - I was curious as to why the
programmer gave this rather complicated example so tried running it on
both Windows and RISC OS. When I got a different answer I began to doubt
my understanding of INT(1.234) <> 1.234 which you correctly verified
gives -1 (equiv. TRUE).

> If rounding up, a simpler way might be PRINT INT(1.234 + 0.9999)
> (as long as the number of 9's exceeds the digits after the decimal
> point)

Agreed.

> A common requirement is to 'half adjust' ie round to the nearest
> integer, which is usually done by PRINT INT(1.234 + 0.5)

I don't suppose you have some examples. No seriously lets not pursue
this any further - you have helped me enough already.

Many thanks

Richard

```
 0
Richard
12/18/2016 11:09:26 AM
```On 18 Dec, Richard Ashbery wrote in message
<55f05c892abasura@invalid.addr.uk>:

> In article <55f055f4edNews03@avisoft.f9.co.uk>, Martin
> <News03@avisoft.f9.co.uk> wrote:
>
> > A common requirement is to 'half adjust' ie round to the nearest
> > integer, which is usually done by PRINT INT(1.234 + 0.5)
>
> I don't suppose you have some examples. No seriously lets not pursue this
> any further - you have helped me enough already.

You can always hit the Delete key... :-)

In fact, Martin has already given you the example:

PRINT INT(1.234 + 0.5)

That's it.

PRINT INT(value + 0.5)

will always print value rounded to the nearest integer.

--
Steve Fryatt - Leeds, England

http://www.stevefryatt.org.uk/
```
 0
Steve
12/18/2016 12:10:05 PM
```In article <55f05c892abasura@invalid.addr.uk>,
Richard Ashbery <basura@invalid.addr.uk> wrote:

> > A common requirement is to 'half adjust' ie round to the nearest
> > integer, which is usually done by PRINT INT(1.234 + 0.5)

> I don't suppose you have some examples. No seriously lets not pursue
> this any further - you have helped me enough already.

There is a whole science devoted th this problem with lots of choices
available.

The more you look into floating point type maths on a computer it gets
harder to decide what you actually want to acheive.

Even simple computations can give weird results such as NAN's (Not A
Number).

Google for further insight.

Ron

--

MrRonMan@GMail.com
```
 0
Ron
12/18/2016 12:54:28 PM
```In article <mpro.oidr4g01wvh4s02oh.news@stevefryatt.org.uk>, Steve
Fryatt <news@stevefryatt.org.uk> wrote:
> On 18 Dec, Richard Ashbery wrote in message
>     <55f05c892abasura@invalid.addr.uk>:

> > In article <55f055f4edNews03@avisoft.f9.co.uk>, Martin
> > <News03@avisoft.f9.co.uk> wrote:
> >
> > > A common requirement is to 'half adjust' ie round to the
> > > nearest integer, which is usually done by PRINT INT(1.234 + 0.5)
> >
> > I don't suppose you have some examples. No seriously lets not
> > pursue this any further - you have helped me enough already.

> You can always hit the Delete key... :-)

:-)

> In fact, Martin has already given you the example:

>   PRINT INT(1.234 + 0.5)

> That's it.

>   PRINT INT(value + 0.5)

> will always print value rounded to the nearest integer.

```
 0
Richard
12/18/2016 3:44:08 PM
```In article <55f06627bdRon@GMail.com>,
Ron <Ron@GMail.com> wrote:

> Even simple computations can give weird results such as NAN's (Not A
> Number).

Depends if you are a Prisoner or not.  ;-)

Dave

--

Dave Triffid
```
 0
Dave
12/18/2016 4:15:14 PM
```Martin wrote:
> So are you really asking: how do I round up a real number to the next
> whole integer? Or something more obscure.
>
> If rounding up, a simpler way might be
>     PRINT INT(1.234 + 0.9999)
> (as long as the number of 9's exceeds the digits after the decimal point)

I think this works, too:

PRINT -INT(-float)

INT with negative numbers rounds up, towards zero.

```
 0
Steve
12/18/2016 4:15:57 PM
```On 18/12/2016 16:15, Steve Drain wrote:
> Martin wrote:
>> So are you really asking: how do I round up a real number to the next
>> whole integer? Or something more obscure.
>>
>> If rounding up, a simpler way might be
>>     PRINT INT(1.234 + 0.9999)
>> (as long as the number of 9's exceeds the digits after the decimal point)
>
> I think this works, too:
>
>   PRINT -INT(-float)
>
> INT with negative numbers rounds up, towards zero.

Blast! I meant to say:

> INT with negative numbers also rounds down, away from zero.
```
 0
Steve
12/18/2016 4:18:40 PM
```In article <o36css\$kcm\$2@gioia.aioe.org>, Steve Drain <steve@kappa.me.uk>
wrote:
> On 18/12/2016 16:15, Steve Drain wrote:
> > Martin wrote:
> >> So are you really asking: how do I round up a real number to the next
> >> whole integer? Or something more obscure.
> >>
> >> If rounding up, a simpler way might be PRINT INT(1.234 + 0.9999) (as
> >> long as the number of 9's exceeds the digits after the decimal point)
> >
> > I think this works, too:
> >
> >   PRINT -INT(-float)
> >
> > INT with negative numbers rounds up, towards zero.

> Blast! I meant to say:

>  > INT with negative numbers also rounds down, away from zero.

This is now reminding me of the snags I encountered when developing the
idea of 'bitfreezing' the least significant bits in 'high resolution' LPCM
audio values. The reason being to reduce the size of flac files without
losing any genuine info. The snag is that you need to set all the least
significant bits to zeros, regardless of the sign of the values. So risk
generating a dc offset because the meaning of a zero in these bits depends
on the sign bit.

Jim

--
Electronics  http://www.st-and.ac.uk/~www_pa/Scots_Guide/intro/electron.htm
Armstrong Audio  http://www.audiomisc.co.uk/Armstrong/armstrong.html
Audio Misc  http://www.audiomisc.co.uk/index.html

```
 0
Jim
12/18/2016 4:56:55 PM
```In article <55f078892edave@triffid.co.uk>, Dave Symes
<dave@triffid.co.uk> wrote:
> In article <55f06627bdRon@GMail.com>, Ron <Ron@GMail.com> wrote:

> > Even simple computations can give weird results such as NAN's
> > (Not A Number).

> Depends if you are a Prisoner or not.  ;-)

> Dave

LOL

Richard

```
 0
Richard
12/18/2016 5:04:56 PM
```In article <o36css\$kcm\$2@gioia.aioe.org>, Steve Drain
<steve@kappa.me.uk> wrote:
> On 18/12/2016 16:15, Steve Drain wrote:
> > Martin wrote:
> >> So are you really asking: how do I round up a real number to the
> >> next whole integer? Or something more obscure.
> >>
> >> If rounding up, a simpler way might be PRINT INT(1.234 + 0.9999)
> >> (as long as the number of 9's exceeds the digits after the
> >> decimal point)
> >
> > I think this works, too:
> >
> >   PRINT -INT(-float)
> >
> > INT with negative numbers rounds up, towards zero.

> Blast! I meant to say:

>  > INT with negative numbers also rounds down, away from zero.

-INT(-1) gives 1 so rounds down, away from zero.

I'm becoming esoterically challenged  :-)

Richard

```
 0
Richard
12/18/2016 5:08:42 PM
```In message <55f07d6e27basura@invalid.addr.uk>
Richard Ashbery <basura@invalid.addr.uk> wrote:

> In article <o36css\$kcm\$2@gioia.aioe.org>, Steve Drain
> <steve@kappa.me.uk> wrote:
>> On 18/12/2016 16:15, Steve Drain wrote:
>>> Martin wrote:
>>>> So are you really asking: how do I round up a real number to the
>>>> next whole integer? Or something more obscure.
>>>>
>>>> If rounding up, a simpler way might be PRINT INT(1.234 + 0.9999)
>>>> (as long as the number of 9's exceeds the digits after the
>>>> decimal point)
>>>
>>> I think this works, too:
>>>
>>>   PRINT -INT(-float)
>>>
>>> INT with negative numbers rounds up, towards zero.

>> Blast! I meant to say:

>>> INT with negative numbers also rounds down, away from zero.

> -INT(-1) gives 1 so rounds down, away from zero.

> I'm becoming esoterically challenged  :-)

> Richard

but -INT(-1.3) produces 2, i.e rounds up.

--
Alan Adams, from Northamptonshire
alan@adamshome.org.uk
http://www.nckc.org.uk/
```
 0
Alan
12/18/2016 5:19:36 PM
```In article <036e7ef055.Alan.Adams@ArmX6.adamshome.org.uk>, Alan Adams
<alan@adamshome.org.uk> wrote:
> In message <55f07d6e27basura@invalid.addr.uk> Richard Ashbery
>           <basura@invalid.addr.uk> wrote:

> > In article <o36css\$kcm\$2@gioia.aioe.org>, Steve Drain
> > <steve@kappa.me.uk> wrote:
> >> On 18/12/2016 16:15, Steve Drain wrote:
> >>> Martin wrote:
> >>>> So are you really asking: how do I round up a real number to
> >>>> the next whole integer? Or something more obscure.
> >>>>
> >>>> If rounding up, a simpler way might be PRINT INT(1.234 +
> >>>> 0.9999) (as long as the number of 9's exceeds the digits after
> >>>> the decimal point)
> >>>
> >>> I think this works, too:
> >>>
> >>>   PRINT -INT(-float)
> >>>
> >>> INT with negative numbers rounds up, towards zero.

> >> Blast! I meant to say:

> >>> INT with negative numbers also rounds down, away from zero.

> > -INT(-1) gives 1 so rounds down, away from zero.

> > I'm becoming esoterically challenged  :-)

> > Richard

> but -INT(-1.3) produces 2, i.e rounds up.

That's what I mean about esoteric. Somebody will be along to explain
it.

Richard

```
 0
Richard
12/18/2016 7:10:14 PM
```In message <55f0888e16basura@invalid.addr.uk>
Richard Ashbery <basura@invalid.addr.uk> wrote:

> In article <036e7ef055.Alan.Adams@ArmX6.adamshome.org.uk>, Alan Adams
> <alan@adamshome.org.uk> wrote:
>> In message <55f07d6e27basura@invalid.addr.uk> Richard Ashbery
>>           <basura@invalid.addr.uk> wrote:

>>> In article <o36css\$kcm\$2@gioia.aioe.org>, Steve Drain
>>> <steve@kappa.me.uk> wrote:
>>>> On 18/12/2016 16:15, Steve Drain wrote:
>>>>> Martin wrote:
>>>>>> So are you really asking: how do I round up a real number to
>>>>>> the next whole integer? Or something more obscure.
>>>>>>
>>>>>> If rounding up, a simpler way might be PRINT INT(1.234 +
>>>>>> 0.9999) (as long as the number of 9's exceeds the digits after
>>>>>> the decimal point)
>>>>>
>>>>> I think this works, too:
>>>>>
>>>>>   PRINT -INT(-float)
>>>>>
>>>>> INT with negative numbers rounds up, towards zero.

>>>> Blast! I meant to say:

>>>>> INT with negative numbers also rounds down, away from zero.

>>> -INT(-1) gives 1 so rounds down, away from zero.

>>> I'm becoming esoterically challenged  :-)

>>> Richard

>> but -INT(-1.3) produces 2, i.e rounds up.

> That's what I mean about esoteric. Somebody will be along to explain
> it.

> Richard

OK. INT always rounds down, i.e. towards more negative values, or less
positive values. Frequently people want one of two other behaviours.

One is rounding, i.e. to the nearest integer. This is usually done
with INT(variable + 0.5). Thus 1.3 would result in INT(1.8) which
returns 1. 1.6 would result in INT(2.1) which returns 2.

The other is returning a number closer to 0. This is the same as INT
for positive values, but not the same for negative values.

For negative numbers -INT(-<value>) works. However it doesn't work
when value is positive, so a test for the sign is needed.

One solution is SGN(value) * INT(ABS(value))
SGN returns 1 for positive values, -1 for negative and 0 for 0.
ABS value returns the valuie as a positive number, the INT truncates
it to an integer. Multiplying them restores the sign to the result.
This then returns the integer closer to zero. For nearest integer, for
both positive and negative numbers you could use

SGN(value) * INT(ABS(value)+0.5)

To make things more manageable I would use

DEF FNround(I)
=SGN(I) * INT(ABS(I)+0.5)

And use FNround(J) to return a rounded value for J

You should also be aware that using integer variables - those whose
names end in % - does an implicit INT function when its value is
assigned. i.e. I%=1.3 results in 1, I%=-1.3 results in -2.

--
Alan Adams, from Northamptonshire
alan@adamshome.org.uk
http://www.nckc.org.uk/
```
 0
Alan
12/18/2016 7:36:35 PM
```Martin wrote:
> So are you really asking: how do I round up a real number to the next
> whole integer? If rounding up, a simpler way might be PRINT INT(1.234
> + 0.9999) (as long as the number of 9's exceeds the digits after

> the decimal point)

In reply, and specifically relating to rounding up:

Steve Drain wrote:
> I think this works, too:
> PRINT -INT(-float)

This has got a bit out of hand, which is my fault for leaving the
explanation as 'an exercise for the reader'. ;-(

Alan Adams wrote:
> The other is returning a number closer to 0. This is the same as INT
> for positive values, but not the same for negative values.
>
> For negative numbers -INT(-<value>) works. However it doesn't work
> when value is positive, so a test for the sign is needed.

For me, the use of that expression was solely for rounding up, away from
zero and both positive and negative values.

> You should also be aware that using integer variables - those whose

> names end in % - does an implicit INT function when its value is

> assigned. i.e. I%=1.3 results in 1, I%=-1.3 results in -2.

That is not correct. Assigning to an integer variable strips the
floating point part [1], so effectively rounds towards zero, I%=-1.3
results in -1, not -2.

So, rounding towards zero is much more simply achieved by assigning a
float to an integer variable.

i%=<float>
PRINT i%

> [snip]

> DEF FNround(I)
> =SGN(I) * INT(ABS(I)+0.5)

If you want it in a function:

DEF FNround(i%)=i%
PRINT FNround(<float>)

[1] A necessary warning is needed about possible odd results owing to
the imprecision of the floating point representation.
```
 0
Steve
12/19/2016 10:50:17 AM
 Reply: