comp.lang.ada
 help / color / mirror / Atom feed
* Modify value - type duration
@ 2008-11-05 21:50 Andreas.Schmidl
  2008-11-05 22:47 ` Jeffrey R. Carter
  0 siblings, 1 reply; 7+ messages in thread
From: Andreas.Schmidl @ 2008-11-05 21:50 UTC (permalink / raw)


Hello!

I started programming in Ada for about a month. Because of that my
knowledge isn't really good, but will be better every day ;-)
I have a problem to modify a fixed point value  (type duration).

I want to cut all before the point. Example:
---------------------------------------------------
var1 : duration := 12.345;
var2 : duration;

var2 := cutIt(num => var1);
---------------------------------------------------

var2 is now 0.345. To implement the function "cutIt" I tested an idea.
I converte var1 to an integer:

---------------------------------------------------
function cutIt(var : duration) return duration is

tempvar : integer;

begin
	tempvar := integer(var);
	return (var - duration(tempvar));

end cutIt;
--------------------------------------------------

But this isn't a solution. Converting to integer with integer(...)
round the value.
Example:
12.345 ---> 12
12.854 ---> 13

Is there any other more elegant and functional solution in Ada?

Thanks for your help!

Best regards!

Andreas



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

* Re: Modify value - type duration
  2008-11-05 21:50 Modify value - type duration Andreas.Schmidl
@ 2008-11-05 22:47 ` Jeffrey R. Carter
  2008-11-06  1:03   ` Adam Beneschan
  0 siblings, 1 reply; 7+ messages in thread
From: Jeffrey R. Carter @ 2008-11-05 22:47 UTC (permalink / raw)


Andreas.Schmidl@googlemail.com wrote:
> function cutIt(var : duration) return duration is
> 
> tempvar : integer;
> 
> begin
> 	tempvar := integer(var);
> 	return (var - duration(tempvar));
> 
> end cutIt;
> 
> But this isn't a solution. Converting to integer with integer(...)
> round the value.
> Example:
> 12.345 ---> 12
> 12.854 ---> 13
> 
> Is there any other more elegant and functional solution in Ada?

Back in the good old days of Ada 83, we'd subtract 0.5 from the value before 
converting to an integer type.

Note that a Duration may contain a value that cannot be converted to Integer.

These days, you can use an appropriate floating-point type and the 'Truncation 
attribute function to get the integer part:

function Cut_It (Value : in Duration) return Duration is
    type Big is digits System.Max_Digits;
begin -- Cut_It
    return Value - Duration (Big'Truncation (Big (Value) ) );
end Cut_It;

-- 
Jeff Carter
"You a big nose have it."
Never Give a Sucker an Even Break
107



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

* Re: Modify value - type duration
  2008-11-05 22:47 ` Jeffrey R. Carter
@ 2008-11-06  1:03   ` Adam Beneschan
  2008-11-06  1:57     ` Jeffrey R. Carter
  0 siblings, 1 reply; 7+ messages in thread
From: Adam Beneschan @ 2008-11-06  1:03 UTC (permalink / raw)


On Nov 5, 2:47 pm, "Jeffrey R. Carter"
<spam.jrcarter....@spam.acm.org> wrote:
> Andreas.Schm...@googlemail.com wrote:
> > function cutIt(var : duration) return duration is
>
> > tempvar : integer;
>
> > begin
> >    tempvar := integer(var);
> >    return (var - duration(tempvar));
>
> > end cutIt;
>
> > But this isn't a solution. Converting to integer with integer(...)
> > round the value.
> > Example:
> > 12.345 ---> 12
> > 12.854 ---> 13
>
> > Is there any other more elegant and functional solution in Ada?
>
> Back in the good old days of Ada 83, we'd subtract 0.5 from the value before
> converting to an integer type.
>
> Note that a Duration may contain a value that cannot be converted to Integer.
>
> These days, you can use an appropriate floating-point type and the 'Truncation
> attribute function to get the integer part:
>
> function Cut_It (Value : in Duration) return Duration is
>     type Big is digits System.Max_Digits;
> begin -- Cut_It
>     return Value - Duration (Big'Truncation (Big (Value) ) );
> end Cut_It;

Hmmm ... now I wonder why there isn't a Truncation attribute for fixed-
point types?  Maybe nobody before Andreas ever thought they'd need
one.  Might be useful.  (Also Floor, Ceiling.)

A possible problem with the floating-point conversion solution is that
there may be cases where you lose accuracy---if, e.g., the largest
floating-point type and fixed-point systems supported on the machine
are both 64 bits.  Since a floating-point number needs room for an
exponent, it's possible to define a fixed-point type with more bits in
the integer part than can be represented in the mantissa of a floating-
point type, e.g.

   type Pathological is delta 0.25 range -(2.0**61) .. (2.0**61-0.25);

although I don't think that would be a problem with the Duration type.

                             -- Adam



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

* Re: Modify value - type duration
  2008-11-06  1:03   ` Adam Beneschan
@ 2008-11-06  1:57     ` Jeffrey R. Carter
  2008-11-06 21:14       ` Keith Thompson
  2008-11-07  1:33       ` Randy Brukardt
  0 siblings, 2 replies; 7+ messages in thread
From: Jeffrey R. Carter @ 2008-11-06  1:57 UTC (permalink / raw)


Adam Beneschan wrote:
> 
> Hmmm ... now I wonder why there isn't a Truncation attribute for fixed-
> point types?  Maybe nobody before Andreas ever thought they'd need
> one.  Might be useful.  (Also Floor, Ceiling.)

No, I wondered about that long ago.

> A possible problem with the floating-point conversion solution is that
> there may be cases where you lose accuracy---if, e.g., the largest
> floating-point type and fixed-point systems supported on the machine
> are both 64 bits.  Since a floating-point number needs room for an
> exponent, it's possible to define a fixed-point type with more bits in
> the integer part than can be represented in the mantissa of a floating-
> point type, e.g.

One must always be careful in such cases that the conversion will not fail or 
lose important bits.

-- 
Jeff Carter
"You a big nose have it."
Never Give a Sucker an Even Break
107



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

* Re: Modify value - type duration
  2008-11-06  1:57     ` Jeffrey R. Carter
@ 2008-11-06 21:14       ` Keith Thompson
  2008-11-07  1:33       ` Randy Brukardt
  1 sibling, 0 replies; 7+ messages in thread
From: Keith Thompson @ 2008-11-06 21:14 UTC (permalink / raw)


"Jeffrey R. Carter" <spam.jrcarter.not@spam.acm.org> writes:
> Adam Beneschan wrote:
>> Hmmm ... now I wonder why there isn't a Truncation attribute for
>> fixed-
>> point types?  Maybe nobody before Andreas ever thought they'd need
>> one.  Might be useful.  (Also Floor, Ceiling.)
>
> No, I wondered about that long ago.
>
>> A possible problem with the floating-point conversion solution is that
>> there may be cases where you lose accuracy---if, e.g., the largest
>> floating-point type and fixed-point systems supported on the machine
>> are both 64 bits.  Since a floating-point number needs room for an
>> exponent, it's possible to define a fixed-point type with more bits in
>> the integer part than can be represented in the mantissa of a floating-
>> point type, e.g.
>
> One must always be careful in such cases that the conversion will not
> fail or lose important bits.

Well, you *could* do an Unchecked_Conversion to an appropriate integer
type, use integer arithmetic to truncate, and then convert back to
Duration.  It's ugly, but it should work on most systems, and it
avoids any accuracy problems.

Doing this in a way that will work under one specific compiler is
fairly straightforward.  Doing it reasonably portably is harder.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"



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

* Re: Modify value - type duration
  2008-11-06  1:57     ` Jeffrey R. Carter
  2008-11-06 21:14       ` Keith Thompson
@ 2008-11-07  1:33       ` Randy Brukardt
  2008-11-07 11:10         ` Ludovic Brenta
  1 sibling, 1 reply; 7+ messages in thread
From: Randy Brukardt @ 2008-11-07  1:33 UTC (permalink / raw)


"Jeffrey R. Carter" <spam.jrcarter.not@spam.acm.org> wrote in message 
news:nasQk.422242$yE1.14213@attbi_s21...
> Adam Beneschan wrote:
>>
>> Hmmm ... now I wonder why there isn't a Truncation attribute for fixed-
>> point types?  Maybe nobody before Andreas ever thought they'd need
>> one.  Might be useful.  (Also Floor, Ceiling.)
>
> No, I wondered about that long ago.
>
>> A possible problem with the floating-point conversion solution is that
>> there may be cases where you lose accuracy---if, e.g., the largest
>> floating-point type and fixed-point systems supported on the machine
>> are both 64 bits.  Since a floating-point number needs room for an
>> exponent, it's possible to define a fixed-point type with more bits in
>> the integer part than can be represented in the mantissa of a floating-
>> point type, e.g.
>
> One must always be careful in such cases that the conversion will not fail 
> or lose important bits.

Right, in both cases. Sounds like someone should propose something on 
Ada-Comment. (This seems like the sort of small cleanup that could be added 
to the next Amendment.)

                            Randy.





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

* Re: Modify value - type duration
  2008-11-07  1:33       ` Randy Brukardt
@ 2008-11-07 11:10         ` Ludovic Brenta
  0 siblings, 0 replies; 7+ messages in thread
From: Ludovic Brenta @ 2008-11-07 11:10 UTC (permalink / raw)


Randy Brukardt wrote:
> Jeffrey R. Carter wrote:
> > Adam Beneschan wrote:
>
> >> Hmmm ... now I wonder why there isn't a Truncation attribute for fixed-
> >> point types?  Maybe nobody before Andreas ever thought they'd need
> >> one.  Might be useful.  (Also Floor, Ceiling.)
>
> > No, I wondered about that long ago.

There is even an Ada Issue from John English about this:
http://www.ada-auth.org/cgi-bin/cvsweb.cgi/AIs/AI-00060.TXT?rev=1.2

--
Ludovic Brenta.



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

end of thread, other threads:[~2008-11-07 11:10 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-11-05 21:50 Modify value - type duration Andreas.Schmidl
2008-11-05 22:47 ` Jeffrey R. Carter
2008-11-06  1:03   ` Adam Beneschan
2008-11-06  1:57     ` Jeffrey R. Carter
2008-11-06 21:14       ` Keith Thompson
2008-11-07  1:33       ` Randy Brukardt
2008-11-07 11:10         ` Ludovic Brenta

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