comp.lang.ada
 help / color / mirror / Atom feed
* Converting numbers to strings without Ada.Text_IO?
@ 2004-04-08 17:25 Tapio Kelloniemi
  2004-04-08 18:14 ` Martin Dowie
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Tapio Kelloniemi @ 2004-04-08 17:25 UTC (permalink / raw)


Hi

I'm writing a collection of generic packages to format numbers (similar to
Ada.Text_IO.xxx_IO). The reason why I don't use the strings-versions
of
procedures under Ada.Text_IO are:
- 'With'ing Ada.Text_IO has side-efects on some systems
   (eg. information about unhandled exceptions, adding blank lines to
   terminal at program termination...)
- Ada.Text_IO has many features which I don't need.
- Number formatting routines in Text_IO only accept bases in range 2 .. 16
  (I know that higher bases are very seldom needed).
- Formatting routines of Text_IO are procedures which prevents
  determining the result strings' lengths in assignment.
- I want to add some features which Ada.Text_IO misses.

Well, my question is: how could in convert integer and real numbers
portably into strings without using Ada.Text_IO?

My idea is to convert variables of generic integer and real types to
some type that can represent all possible integers or reals on that
system and then recursively extracting the digits by dividing by base
etc. Are there some issues that I should be aware of?

Attempt:

type Max_Integer is new range System.Min_Int .. System.Max_Int;
type Max_Real is digits System.Max_Digits;
...
--  Returns the last digit of an integer number as character.
--  This is not a final form, but is it correct or very inefficient?
function Last_Digit (N : Number) return Character is
begin
   if abs N >= Base then
      return To_Char (Last_Digit (abs (N / Base)));
   else
      return To_Char (abs N);
   end if;
end Last_Digit;

-- 
Tapio



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

* Re: Converting numbers to strings without Ada.Text_IO?
  2004-04-08 17:25 Converting numbers to strings without Ada.Text_IO? Tapio Kelloniemi
@ 2004-04-08 18:14 ` Martin Dowie
  2004-04-09  3:50   ` James Rogers
  2004-04-09  0:06 ` Stephen Leake
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 8+ messages in thread
From: Martin Dowie @ 2004-04-08 18:14 UTC (permalink / raw)


"Tapio Kelloniemi" <spam12@thack.org> wrote in message
news:rWfdc.2825$MA6.804@reader1.news.jippii.net...
> Well, my question is: how could in convert integer and real numbers
> portably into strings without using Ada.Text_IO?

You could copy the algorithm GNAT uses for conversions from Ada.Text_IO into
your own package (with appropriate copyrighting kept)





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

* Re: Converting numbers to strings without Ada.Text_IO?
  2004-04-08 17:25 Converting numbers to strings without Ada.Text_IO? Tapio Kelloniemi
  2004-04-08 18:14 ` Martin Dowie
@ 2004-04-09  0:06 ` Stephen Leake
  2004-04-09  9:58 ` Dmitry A. Kazakov
  2004-04-12  5:13 ` Richard  Riehle
  3 siblings, 0 replies; 8+ messages in thread
From: Stephen Leake @ 2004-04-09  0:06 UTC (permalink / raw)
  To: comp.lang.ada

Tapio Kelloniemi <spam12@thack.org> writes:

> Hi
> 
> I'm writing a collection of generic packages to format numbers
> (similar to Ada.Text_IO.xxx_IO). The reason why I don't use the
> strings-versions of procedures under Ada.Text_IO are:

Good list; snipped for space.

> Well, my question is: how could in convert integer and real numbers
> portably into strings without using Ada.Text_IO?

Martin Dowie suggested copying the core code from GNAT; that's a good
idea.

Here's code I wrote that converts modular types to hexadecimal:

function SAL.Generic_Hex_Image (Item : in Number_Type) return String
is
   Temp : Number_Type := Item;
   Nibble : Number_Type;
   Image : String (1 .. Width);
begin
   for I in reverse Image'Range loop
      Nibble := Temp mod 16;
      Temp := Temp / 16;
      if Nibble > 9 then
         Image (I) := Character'Val (Character'Pos ('A') + Nibble - 10);
      else
         Image (I) := Character'Val (Character'Pos ('0') + Nibble);
      end if;
   end loop;
   return Image;
end Sal.Generic_Hex_Image;

> Are there some issues that I should be aware of?

Lots. Go read the GNAT code.

-- 
-- Stephe




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

* Re: Converting numbers to strings without Ada.Text_IO?
  2004-04-08 18:14 ` Martin Dowie
@ 2004-04-09  3:50   ` James Rogers
  2004-04-09  6:03     ` Martin Dowie
  0 siblings, 1 reply; 8+ messages in thread
From: James Rogers @ 2004-04-09  3:50 UTC (permalink / raw)


"Martin Dowie" <martin.dowie@btopenworld.com> wrote in
news:c544qe$l5d$1@sparta.btinternet.com: 

> "Tapio Kelloniemi" <spam12@thack.org> wrote in message
> news:rWfdc.2825$MA6.804@reader1.news.jippii.net...
>> Well, my question is: how could in convert integer and real numbers
>> portably into strings without using Ada.Text_IO?
> 
> You could copy the algorithm GNAT uses for conversions from
> Ada.Text_IO into your own package (with appropriate copyrighting kept)

You could also simply call the 'Image attribute of the integer or
real number.

Jim Rogers



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

* Re: Converting numbers to strings without Ada.Text_IO?
  2004-04-09  3:50   ` James Rogers
@ 2004-04-09  6:03     ` Martin Dowie
  2004-04-09 12:34       ` James Rogers
  0 siblings, 1 reply; 8+ messages in thread
From: Martin Dowie @ 2004-04-09  6:03 UTC (permalink / raw)


> >> Well, my question is: how could in convert integer and real numbers
> >> portably into strings without using Ada.Text_IO?
> >
> > You could copy the algorithm GNAT uses for conversions from
> > Ada.Text_IO into your own package (with appropriate copyrighting kept)
>
> You could also simply call the 'Image attribute of the integer or
> real number.

My assumption (which could be wrong!) is the OP is using
Ada.Text_IO.* to get access to finer levels of formatting
the 'Image offers.

Cheers

-- Martin





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

* Re: Converting numbers to strings without Ada.Text_IO?
  2004-04-08 17:25 Converting numbers to strings without Ada.Text_IO? Tapio Kelloniemi
  2004-04-08 18:14 ` Martin Dowie
  2004-04-09  0:06 ` Stephen Leake
@ 2004-04-09  9:58 ` Dmitry A. Kazakov
  2004-04-12  5:13 ` Richard  Riehle
  3 siblings, 0 replies; 8+ messages in thread
From: Dmitry A. Kazakov @ 2004-04-09  9:58 UTC (permalink / raw)


Tapio Kelloniemi wrote:

> I'm writing a collection of generic packages to format numbers (similar to
> Ada.Text_IO.xxx_IO).

...

> Well, my question is: how could in convert integer and real numbers
> portably into strings without using Ada.Text_IO?

You can take a look at my implementation of numeric I/O:

http://www.dmitry-kazakov.de/ada/strings_edit.htm

-- 
Regards,
Dmitry A. Kazakov
www.dmitry-kazakov.de



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

* Re: Converting numbers to strings without Ada.Text_IO?
  2004-04-09  6:03     ` Martin Dowie
@ 2004-04-09 12:34       ` James Rogers
  0 siblings, 0 replies; 8+ messages in thread
From: James Rogers @ 2004-04-09 12:34 UTC (permalink / raw)


"Martin Dowie" <martin.dowie@btopenworld.com> wrote in 
news:c55ec1$gsj$1@sparta.btinternet.com:

>> >> Well, my question is: how could in convert integer and real numbers
>> >> portably into strings without using Ada.Text_IO?
>> >
>> > You could copy the algorithm GNAT uses for conversions from
>> > Ada.Text_IO into your own package (with appropriate copyrighting kept)
>>
>> You could also simply call the 'Image attribute of the integer or
>> real number.
> 
> My assumption (which could be wrong!) is the OP is using
> Ada.Text_IO.* to get access to finer levels of formatting
> the 'Image offers.

The Ada.Text_Io package provides the ability to convert between
number bases for integers. Other than that, there is no finer formatting
needed. Once a string version of an integer or real type is
created the programmer can parse and modify that string for any
additional formatting requirements.

Jim Rogers



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

* Re: Converting numbers to strings without Ada.Text_IO?
  2004-04-08 17:25 Converting numbers to strings without Ada.Text_IO? Tapio Kelloniemi
                   ` (2 preceding siblings ...)
  2004-04-09  9:58 ` Dmitry A. Kazakov
@ 2004-04-12  5:13 ` Richard  Riehle
  3 siblings, 0 replies; 8+ messages in thread
From: Richard  Riehle @ 2004-04-12  5:13 UTC (permalink / raw)



"Tapio Kelloniemi" <spam12@thack.org> wrote in message
news:rWfdc.2825$MA6.804@reader1.news.jippii.net...

> My idea is to convert variables of generic integer and real types to
> some type that can represent all possible integers or reals on that
> system and then recursively extracting the digits by dividing by base
> etc. Are there some issues that I should be aware of?

This question comes up so often among novices to Ada that I
included an extended example of it in Ada Distilled.  The examples
are in valid source code and each line of code is commented.

You can download Ada Distilled from http://www.adaic.org along with
a Zip file with all the programs in electronic form so you can experiment
with them.

Richard Riehle






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

end of thread, other threads:[~2004-04-12  5:13 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-04-08 17:25 Converting numbers to strings without Ada.Text_IO? Tapio Kelloniemi
2004-04-08 18:14 ` Martin Dowie
2004-04-09  3:50   ` James Rogers
2004-04-09  6:03     ` Martin Dowie
2004-04-09 12:34       ` James Rogers
2004-04-09  0:06 ` Stephen Leake
2004-04-09  9:58 ` Dmitry A. Kazakov
2004-04-12  5:13 ` Richard  Riehle

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