comp.lang.ada
 help / color / mirror / Atom feed
* Rounded off
@ 1999-06-09  0:00 dginc
  1999-06-09  0:00 ` dennison
  0 siblings, 1 reply; 7+ messages in thread
From: dginc @ 1999-06-09  0:00 UTC (permalink / raw)


i am wondering how to have my output come out rounded insted of with 
decimals??  the output is 19.8 figured from calculation, i want it to output 
that rounded off (20).  the calculation is from float a decimal and input is a 
whole number.  any suggestions




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Rounded off
  1999-06-09  0:00 Rounded off dginc
@ 1999-06-09  0:00 ` dennison
  1999-06-09  0:00   ` David C. Hoos, Sr.
  1999-06-10  0:00   ` Robert Dewar
  0 siblings, 2 replies; 7+ messages in thread
From: dennison @ 1999-06-09  0:00 UTC (permalink / raw)


In article <jKz73.34$ep6.34762@ratbert.tds.net>,
  dginc@tds.net wrote:
> i am wondering how to have my output come out rounded insted of with
> decimals??  the output is 19.8 figured from calculation, i want it to
output
> that rounded off (20).  the calculation is from float a decimal and
input is a
> whole number.  any suggestions
>


How about:
 Integer(X + 0.5)


--
T.E.D.


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Rounded off
  1999-06-09  0:00 ` dennison
@ 1999-06-09  0:00   ` David C. Hoos, Sr.
  1999-06-10  0:00     ` Robert Dewar
  1999-06-10  0:00   ` Robert Dewar
  1 sibling, 1 reply; 7+ messages in thread
From: David C. Hoos, Sr. @ 1999-06-09  0:00 UTC (permalink / raw)



<dennison@telepath.com> wrote in message news:7jmq0c$uq$1@nnrp1.deja.com...
> In article <jKz73.34$ep6.34762@ratbert.tds.net>,
>   dginc@tds.net wrote:
> > i am wondering how to have my output come out rounded insted of with
> > decimals??  the output is 19.8 figured from calculation, i want it to
> output
> > that rounded off (20).  the calculation is from float a decimal and
> input is a
> > whole number.  any suggestions
> >
> 
> 
> How about:
>  Integer(X + 0.5)
>
The above suggestion will give wrong results for negative numbers.

Real values are already rounded on conversion to Integer (RM 4.6 (33),
viz.:
If the target type is an integer type and the operand type is real, the
result is rounded to the nearest integer (away from zero if exactly
halfway between two integers).

So simply Integer (X) will give correct results for all values of X
within the range of type Integer.

For rounding to, say, the nearest 0.01, one should use an instantiation
of the appropriate generic package from Ada.Text_IO, and use an Aft
value of 2 when calling the Put procedure.  Rounding is done by the
Put procedure. 







^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Rounded off
  1999-06-09  0:00 ` dennison
  1999-06-09  0:00   ` David C. Hoos, Sr.
@ 1999-06-10  0:00   ` Robert Dewar
  1999-06-14  0:00     ` William Dale
  1999-06-15  0:00     ` czgrr
  1 sibling, 2 replies; 7+ messages in thread
From: Robert Dewar @ 1999-06-10  0:00 UTC (permalink / raw)


In article <7jmq0c$uq$1@nnrp1.deja.com>,
  dennison@telepath.com wrote:
> How about:
>  Integer(X + 0.5)
>
> --
> T.E.D.

Ouch! That's a mistake! The proper answer for rounding float
to integer is of course

  Integer (X)



Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Rounded off
  1999-06-09  0:00   ` David C. Hoos, Sr.
@ 1999-06-10  0:00     ` Robert Dewar
  0 siblings, 0 replies; 7+ messages in thread
From: Robert Dewar @ 1999-06-10  0:00 UTC (permalink / raw)


In article <mozxXkus#GA.225@newstoo.hiwaay.net>,
  "David C. Hoos, Sr." <david.c.hoos.sr@ada95.com> wrote:
> > How about:
> >  Integer(X + 0.5)
> >
> > The above suggestion will give wrong results for negative
> > numbers.

NO! it is worse than that, it gives wrong results for half of
all positive and negative numbers, for example

   Integer (2.3 + 0.5)

is

3!



Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Rounded off
  1999-06-10  0:00   ` Robert Dewar
@ 1999-06-14  0:00     ` William Dale
  1999-06-15  0:00     ` czgrr
  1 sibling, 0 replies; 7+ messages in thread
From: William Dale @ 1999-06-14  0:00 UTC (permalink / raw)


Robert Dewar wrote:
> 
> In article <7jmq0c$uq$1@nnrp1.deja.com>,
>   dennison@telepath.com wrote:
> > How about:
> >  Integer(X + 0.5)
> >
> > --
> > T.E.D.
> 
> Ouch! That's a mistake! The proper answer for rounding float
> to integer is of course
> 
>   Integer (X)
> 
> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.


You might also look up 

	T'Truncate (X) 
	T'Ceiling (X)
	T'Floor (X) 

All take a float and return the same float type.


-- 

"The difference between hardware and software is that the more you play
with hardware, the more likely you are to break it, but the more you
play with software the more likely you are to FIX it."

Bill Dale 
LMMS
mailto:william.dale.jr@lmco.com
mailto:N2RHV@amsat.org




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Rounded off
  1999-06-10  0:00   ` Robert Dewar
  1999-06-14  0:00     ` William Dale
@ 1999-06-15  0:00     ` czgrr
  1 sibling, 0 replies; 7+ messages in thread
From: czgrr @ 1999-06-15  0:00 UTC (permalink / raw)


In article <7jn9af$63i$1@nnrp1.deja.com>,
  Robert Dewar <robert_dewar@my-deja.com> wrote:
> [snip] The proper answer for rounding float
> to integer is of course
>
>   Integer (X)
>
An additional note on this for those of you who, like me, still use
Ada83.

This is the text of a warning message from ObjectAda when the above
statement is compiled with the Ada83 error checking enabled...

simulation_programs.adb: Ada 83 Warning: line 945 col 42 LRM:4.6(33),
Rounding of Float to Integer now rounds away from 0, Continuing
Ada Compatibility Guide Number 16,
Notes:  Programs should not depend on how rounding is done
        and in Ada83 it was implementation-dependent.  In
        Ada9X it is always away from 0.

So if you are using Ada83 you need to check what your compiler does,
and if in doubt, write a function that does it the way you want.

czgrr

--
No email, please - reply to the newsgroup. Email may be posted.
My opinions are not necessarily those of my employer.
My suggestions might not be correct. Use at your own risk.


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~1999-06-15  0:00 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-06-09  0:00 Rounded off dginc
1999-06-09  0:00 ` dennison
1999-06-09  0:00   ` David C. Hoos, Sr.
1999-06-10  0:00     ` Robert Dewar
1999-06-10  0:00   ` Robert Dewar
1999-06-14  0:00     ` William Dale
1999-06-15  0:00     ` czgrr

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox