* Conversions
@ 2000-03-16 0:00 Karlene Johnson
2000-03-16 0:00 ` Conversions Marin D. Condic
0 siblings, 1 reply; 5+ messages in thread
From: Karlene Johnson @ 2000-03-16 0:00 UTC (permalink / raw)
I am trying to convert an Float number to an Integer number, after a
calculation with 2 Float numbers. Any suggesions on how to convert??
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Conversions
2000-03-16 0:00 Conversions Karlene Johnson
@ 2000-03-16 0:00 ` Marin D. Condic
0 siblings, 0 replies; 5+ messages in thread
From: Marin D. Condic @ 2000-03-16 0:00 UTC (permalink / raw)
Karlene Johnson wrote:
>
> I am trying to convert an Float number to an Integer number, after a
> calculation with 2 Float numbers. Any suggesions on how to convert??
X : Float := 1.234 ;
Y : Integer := 0 ;
....
Y := Integer (X) ; -- Y should now contain the value "1"
There are a number of ways of handling truncation & rounding, but this
is the plain vanilla way to do it. The type name acts as a function to
perform the conversion. However, it only works with "compatible" types.
(You can't for example, convert a String to a Float this way.)
MDC
--
=============================================================
Marin David Condic - Quadrus Corporation - 1.800.555.3393
1015-116 Atlantic Boulevard, Atlantic Beach, FL 32233
http://www.quadruscorp.com/
m c o n d i c @ q u a d r u s c o r p . c o m
***PLEASE REMOVE THE "-NOSPAM" PART OF MY RETURN ADDRESS***
Visit my web site at: http://www.mcondic.com/
"Because that's where they keep the money."
-- Willie Sutton when asked why he robbed banks.
=============================================================
^ permalink raw reply [flat|nested] 5+ messages in thread
* Ada.Directories.Base_Name and dot files
@ 2009-10-07 12:40
2009-10-08 9:39 ` Stephen Leake
0 siblings, 1 reply; 5+ messages in thread
From: @ 2009-10-07 12:40 UTC (permalink / raw)
Hey all,
While working my way through the excellent Ada.Directories package, I've
stumbled on a minor annoyance: The Base_Name function does not handle
dot files very gracefully.
Here's an example:
++++
with Ada.Text_IO;
with Ada.Directories;
procedure Dot is
package IO renames Ada.Text_IO;
package D renames Ada.Directories;
begin
IO.Put_Line (Item => "1: " & D.Base_Name (Name => "foo.txt"));
IO.Put_Line (Item => "2: " & D.Base_Name (Name => ".foo"));
IO.Put_Line (Item => "3: " & D.Base_Name (Name => ".foo.bar"));
end Dot;
++++
The output I get is this:
++++
1: foo
2:
3: .foo
++++
What I had hoped for, was this:
++++
1: foo
2: .foo
3: .foo
++++
With the current null string solution, you'd have to do some manual
parsing to get to the actual basename of a dot file.
Instead of returning a null string when the first character is a dot,
couldn't Base_Name just as well just return the basename *with* the dot,
because that is the actual basename for a dot file. The dot is part of
the name.
Is there a good reason for the current behavior?
There's a comment in the body of Base_Name:
++++
-- Look for the last dot in the file name and return the part of the
-- file name preceding this last dot. If the first dot is the first
-- character of the file name, the base name is the empty string.
++++
So it's not like the programmers behind this wasn't aware of dot files,
they've just opted for a somewhat odd solution, in my humble, and not
very expert, opinion. :o)
--
Regards,
Thomas L�cke
Email: tl at ada-dk.org
Web: http:ada-dk.org
IRC nick: ThomasLocke
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Ada.Directories.Base_Name and dot files
2009-10-07 12:40 Ada.Directories.Base_Name and dot files
@ 2009-10-08 9:39 ` Stephen Leake
2009-10-08 9:58 `
0 siblings, 1 reply; 5+ messages in thread
From: Stephen Leake @ 2009-10-08 9:39 UTC (permalink / raw)
Thomas L�cke <"tl at ada-dk.org"> writes:
> begin
> IO.Put_Line (Item => "1: " & D.Base_Name (Name => "foo.txt"));
> IO.Put_Line (Item => "2: " & D.Base_Name (Name => ".foo"));
> IO.Put_Line (Item => "3: " & D.Base_Name (Name => ".foo.bar"));
> end Dot;
> ++++
>
> The output I get is this:
>
> ++++
> 1: foo
> 2:
> 3: .foo
> ++++
>
> What I had hoped for, was this:
>
> ++++
> 1: foo
> 2: .foo
> 3: .foo
> ++++
Just out of curiosity, what made you hope for this? The Gnu coreutils
basename function doesn't do what you want, either:
$ basename foo.txt
foo.txt
$ basename foo.txt .txt
foo
$ basename .foo
.foo
$ basename .foo .foo
.foo
$ basename .foo.bar
.foo.bar
$ basename .foo.bar .bar
.foo
$ basename .foo.bar .foo
.foo.bar
$ basename .foo.bar .foo.bar
.foo.bar
> With the current null string solution, you'd have to do some manual
> parsing to get to the actual basename of a dot file.
What is your definition of "the actual basename"?
> Is there a good reason for the current behavior?
It's well-defined. And it does what I want :).
--
-- Stephe
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Ada.Directories.Base_Name and dot files
2009-10-08 9:39 ` Stephen Leake
@ 2009-10-08 9:58 `
2009-10-26 19:27 ` Conversions Bruno
0 siblings, 1 reply; 5+ messages in thread
From: @ 2009-10-08 9:58 UTC (permalink / raw)
Stephen Leake wrote:
> Thomas L�cke <"tl at ada-dk.org"> writes:
>
>> begin
>> IO.Put_Line (Item => "1: " & D.Base_Name (Name => "foo.txt"));
>> IO.Put_Line (Item => "2: " & D.Base_Name (Name => ".foo"));
>> IO.Put_Line (Item => "3: " & D.Base_Name (Name => ".foo.bar"));
>> end Dot;
>> ++++
>>
>> The output I get is this:
>>
>> ++++
>> 1: foo
>> 2:
>> 3: .foo
>> ++++
>>
>> What I had hoped for, was this:
>>
>> ++++
>> 1: foo
>> 2: .foo
>> 3: .foo
>> ++++
>
> Just out of curiosity, what made you hope for this? The Gnu coreutils
> basename function doesn't do what you want, either:
Yes, that was actually a typo. I was hoping for the same behavior as
basename, as shown in your output below.
In my haste, I just forgot to add the .bar part to output line 3: .foo
I was too focused on the second line, where .foo was completely
eliminated.
>
> $ basename foo.txt
> foo.txt
> $ basename foo.txt .txt
> foo
> $ basename .foo
> .foo
> $ basename .foo .foo
> .foo
> $ basename .foo.bar
> .foo.bar
> $ basename .foo.bar .bar
> .foo
> $ basename .foo.bar .foo
> .foo.bar
> $ basename .foo.bar .foo.bar
> .foo.bar
>
>
>> With the current null string solution, you'd have to do some manual
>> parsing to get to the actual basename of a dot file.
>
> What is your definition of "the actual basename"?
On my Linux system, my definition of the basename, is the name returned
by basename.
>
>> Is there a good reason for the current behavior?
>
> It's well-defined. And it does what I want :).
>
I've read the link posted by Adam, and it has made me a lot wiser in
regards to this issue. I feel I now have a better understanding of why
Ada.Directories.Base_Name does what it does.
So I guess I now also think that it is both well-defined and that it
does what I didn't knew I wanted. :o)
--
Regards,
Thomas L�cke
Email: tl at ada-dk.org
Web: http:ada-dk.org
IRC nick: ThomasLocke
^ permalink raw reply [flat|nested] 5+ messages in thread
* Conversions
2009-10-08 9:58 `
@ 2009-10-26 19:27 ` Bruno
2009-10-26 20:09 ` Conversions Jeffrey R. Carter
2009-10-26 20:19 ` Conversions Adam Beneschan
0 siblings, 2 replies; 5+ messages in thread
From: Bruno @ 2009-10-26 19:27 UTC (permalink / raw)
Hi all,
I have a little problem with a function that "convert" a date, that is a
record type, into a string. To do this I'm using "image" attribute how
can you see in the following code
http://paste.ideaslabs.com/show/5KEJUGcDF
but when run the program and the function is executed a get an
"constraint error". The compiler show me a message that say that the
size is too long but I don't know what to do.
How can I repair this error?
Thanks,
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Conversions
2009-10-26 19:27 ` Conversions Bruno
@ 2009-10-26 20:09 ` Jeffrey R. Carter
2009-10-26 20:19 ` Conversions Adam Beneschan
1 sibling, 0 replies; 5+ messages in thread
From: Jeffrey R. Carter @ 2009-10-26 20:09 UTC (permalink / raw)
Bruno wrote:
>
> I have a little problem with a function that "convert" a date, that is a
> record type, into a string. To do this I'm using "image" attribute how
> can you see in the following code
>
> http://paste.ideaslabs.com/show/5KEJUGcDF
>
> but when run the program and the function is executed a get an
> "constraint error". The compiler show me a message that say that the
> size is too long but I don't know what to do.
'Image for an integer type returns the shortest string necessary to represent
the value, with ' ' added to the front for a positive value and '-' for a
negative value.
Thus we can get
6 => " 6"
-6 => "-6"
10 => " 10"
-10 => "-10"
For your application (date image), 2-digit day and month numbers are likely;
their 'Image will not fit in 2 characters.
You can save the 'Image and decide what to do with it:
declare
Day_Image : constant String := Integer'Image (F.Dia);
begin
if F.Dia < 10 then
S (1) := '0';
S (2) := Day_Image (Day_Image'Last);
else
S (1 .. 2) := Day_Image (Day_Image'Last - 1 .. Day_Image'Last);
end if;
end;
You can also look at the operations in Ada.Text_IO.Integer_IO that write to a
String parameter.
--
Jeff Carter
"We call your door-opening request a silly thing."
Monty Python & the Holy Grail
17
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Conversions
2009-10-26 19:27 ` Conversions Bruno
2009-10-26 20:09 ` Conversions Jeffrey R. Carter
@ 2009-10-26 20:19 ` Adam Beneschan
1 sibling, 0 replies; 5+ messages in thread
From: Adam Beneschan @ 2009-10-26 20:19 UTC (permalink / raw)
On Oct 26, 12:27 pm, Bruno <brunomend...@gmail.com> wrote:
> Hi all,
>
> I have a little problem with a function that "convert" a date, that is a
> record type, into a string. To do this I'm using "image" attribute how
> can you see in the following code
>
> http://paste.ideaslabs.com/show/5KEJUGcDF
>
> but when run the program and the function is executed a get an
> "constraint error". The compiler show me a message that say that the
> size is too long but I don't know what to do.
'Image returns a string whose first character is a space if the
argument is not negative. So if N = 12, then Integer'Image(N) = "
12"---i.e. 3 characters. That won't fit in the two-character slice
you're trying to put it in, so you get a Constraint_Error.
But just getting rid of the space doesn't work either. If N = 1, then
Integer'Image(N) = " 1", and if you were able to trim off the space,
the result would be "1", and you'd still get a Constraint_Error. When
you assign a slice like this, the left and right sides must be the
same length.
There are a number of ways to do what you want, but I'd suggest
looking at the Ada.Strings.Fixed package, especially the Move, Trim,
and Tail routines.
Hope this helps,
-- Adam
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2009-10-26 20:19 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-03-16 0:00 Conversions Karlene Johnson
2000-03-16 0:00 ` Conversions Marin D. Condic
-- strict thread matches above, loose matches on Subject: below --
2009-10-07 12:40 Ada.Directories.Base_Name and dot files
2009-10-08 9:39 ` Stephen Leake
2009-10-08 9:58 `
2009-10-26 19:27 ` Conversions Bruno
2009-10-26 20:09 ` Conversions Jeffrey R. Carter
2009-10-26 20:19 ` Conversions Adam Beneschan
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox