From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,f9957894e0bdf128 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,UTF8 Path: g2news2.google.com!news4.google.com!feeder3.cambrium.nl!feeder4.cambrium.nl!feed.tweaknews.nl!193.141.40.65.MISMATCH!npeer.de.kpn-eurorings.net!npeer-ng1.de.kpn-eurorings.net!peernews!feed.news.schlund.de!schlund.de!news.online.de!not-for-mail From: Michael Bode Newsgroups: comp.lang.ada Subject: Re: How to put 200 into an integer sub-type of 16 bits (code included) Date: Thu, 15 Jan 2009 07:53:13 +0100 Organization: 1&1 Internet AG Message-ID: References: <407ae64d-3cb3-4310-b59e-f1bbae9910a5@t39g2000prh.googlegroups.com> <71gqm49eatq868htrvd7eghm3m8su8kcbl@4ax.com> <3d3719f4-355c-4094-9902-495d612d46fe@n33g2000pri.googlegroups.com> <139961e9-bae6-4e60-8ff7-4f4779b27481@z6g2000pre.googlegroups.com> <87816592-c947-4bbc-92ed-7473646a105e@a12g2000pro.googlegroups.com> <3625f980-4406-4f51-b494-dd4a48cab840@p36g2000prp.googlegroups.com> NNTP-Posting-Host: p57b6078b.dip0.t-ipconnect.de Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: online.de 1232002393 10965 87.182.7.139 (15 Jan 2009 06:53:13 GMT) X-Complaints-To: abuse@einsundeins.com NNTP-Posting-Date: Thu, 15 Jan 2009 06:53:13 +0000 (UTC) X-message-flag: IMPORTANT MESSAGE -- PLEASE READ IMMEDIATELY!!! X-Accepted-File-Formats: ASCII, .rtf, .ps, .pdf - *NO* MS Office files User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.2 (gnu/linux) Xref: g2news2.google.com comp.lang.ada:4283 Date: 2009-01-15T07:53:13+01:00 List-Id: ChristopherL writes: > The size of floating point number is 32 bits on my system, and my > float will > always be positive. > > General information about floating point numbers in general: > > Floating-point numbers are typically packed into a computer datum as > the sign bit, the exponent field, and the significand (mantissa). > > Mantissa Exponent Value > 71 0 71 > 71 1 710 > 71 2 7100 > 71 -1 7.1 > 2 2 200 > > For example, it was shown above that π, rounded to 24 bits of > precision, has: > > sign = 0 ; e = 1 ; s = 110010010000111111011011 (including the hidden > bit) > The sum of the exponent bias (127) and the exponent (1) is 128, so > this is represented in single precision format as > > 0 10000000 10010010000111111011011 (excluding the hidden bit). > > So, what is the proper way to store a number (never being greater > than > 200.5) in a 8 bit short number as outlined above? Try compressing it with gzip? Your question is how to store 4 bytes in 1 byte. The answer is, you don't. You use 4 8-bit numbers to store 1 32-bit number. In Ada you can do it like this: with Ada.Unchecked_Conversion; with Ada.Text_Io; procedure Convert is type SByte is range -128 .. 127; for SByte'Size use 8; type Float_Bytes is array (0 .. 3) of SByte; Arg : Float := 200.5; Result : Float_Bytes; function To_Bytes is new Ada.Unchecked_Conversion (Source => Float, Target => Float_Bytes); begin Result := To_Bytes (Arg); for I in Result'Range loop Ada.Text_Io.Put (Sbyte'Image (Result(I)) & " "); end loop; Ada.Text_Io.New_Line; end Convert; Output: 0 -128 72 67