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=-0.9 required=5.0 tests=BAYES_00,FROM_NUMERIC_TLD autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,f9957894e0bdf128 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!newsfeed.stanford.edu!news.kjsl.com!news-peer-lilac.gradwell.net!not-for-mail From: "Stuart" Newsgroups: comp.lang.ada 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> <1a2b31ac-cf6b-44e3-85b7-04594460db87@d36g2000prf.googlegroups.com> Subject: Re: How to put 200 into an integer sub-type of 16 bits (code included) Date: Thu, 15 Jan 2009 12:40:38 -0000 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.3138 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3350 Message-ID: <496f2a60$1_1@glkas0286.greenlnk.net> X-Original-NNTP-Posting-Host: glkas0286.greenlnk.net NNTP-Posting-Host: 20.133.0.1 X-Trace: 1232023252 news.gradwell.net 513 dnews/20.133.0.1:40809 X-Complaints-To: news-abuse@gradwell.net Xref: g2news1.google.com comp.lang.ada:3320 Date: 2009-01-15T12:40:38+00:00 List-Id: "ChristopherL" wrote in message news:1a2b31ac-cf6b-44e3-85b7-04594460db87@d36g2000prf.googlegroups.com... > 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? then "Stuart" wrote in message news:496f03e2$1_1@glkas0286.greenlnk.net... > OK - there is no 'proper' way, there are several ways, each of which has > advantages and disadvantages. To add to what I wrote before, and depending on what you are trying to achieve, you might find the following Ada code of interest: with Ada.Unchecked_Conversion; procedure ada_main is type Flt is digits 7 range -1000.0 .. 1000.0; -- !!! or whatever! -- The condition we need to allow the range 0.0..200.0 to be stored with -- most precision in 8 bits is: -- 255 * small > 200.0 - small/2 -- => 255.5 * small > 200.0 -- => small > 200.0 / 255.5 Fxd_Small : constant := 0.782778865; -- !!! There seem to be some issues with GNAT debugger using an expression here! type Fxd is delta Fxd_Small range 0.0 .. 200.0; for Fxd'small use Fxd_Small; for Fxd'size use 8; type Int is mod 256; -- In case you really want the underlying 'integer' representation for Int'size use 8; function to_Int is new Ada.Unchecked_Conversion(Source => Fxd, Target => Int); -- Create some volatile variables to force code to be generated. X : Flt; pragma volatile(X); Y : Fxd; for Y'size use 8; pragma volatile(Y); Z : Int; for Z'size use 8; pragma volatile(Z); begin X := 200.0; Y := Fxd(X); -- 199.6086 -- Note: Read up about model numbers !!! -- This is the closest model number to 200.0 for Fxd. Z := to_Int(Y); -- 255 end ada_main; Regards Stuart