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!news3.google.com!feeder.news-service.com!tudelft.nl!txtfeed1.tudelft.nl!zen.net.uk!dedekind.zen.co.uk!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 09:56:25 -0000 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.3138 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3350 X-RFC2646: Format=Flowed; Original Message-ID: <496f03e2$1_1@glkas0286.greenlnk.net> X-Original-NNTP-Posting-Host: glkas0286.greenlnk.net NNTP-Posting-Host: 20.133.0.1 X-Trace: 1232013392 news.gradwell.net 511 dnews/20.133.0.1:32651 X-Complaints-To: news-abuse@gradwell.net Xref: g2news1.google.com comp.lang.ada:3314 Date: 2009-01-15T09:56:25+00:00 List-Id: "ChristopherL" wrote in message news:1a2b31ac-cf6b-44e3-85b7-04594460db87@d36g2000prf.googlegroups.com... > Please remove the rounding operation for now from this discussion. OK. > This problem was given to me to try to solve who said it's probably > impossible! Not being funny or anything but it might be as well to describe the problem as given. I think the exchanges elsewhere in this thread demonstrate that there is a great deal of confusion. > 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: > 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? OK - there is no 'proper' way, there are several ways, each of which has advantages and disadvantages. I get the impression that you have a 'real' input in the range: 0.0 .. 200.5 and you want to have this value held in an 8-bit variable. You do not say to what precision you need this value, or how this precision might vary over the value range. Floating point representations provide a relative precision, so while values around 200.0 might only be represented with a precision of 1.0, values around 20.0 would be represented with a precision of 0.1, and those around 2.0 with a precision of 0.01. Fixed point representations provide a fixed precision, so all values might be represented with a precision of 1.0 whether they be 200.0, 20.0 or 2.0. You need to clarify what your requirements are! If fixed point precision is adequate to your needs then you should find that Ada fixed point types will meet your needs most adequately (you may need to specify the 'small attribute to extract the maximum amount of precision) and you will be able to use a simple type conversion. If you need floating point precision things are going to be more difficult because you are going to have to roll your own representation. You seem to have done some research on floating point representations so I won't go into detail now, but I would note that: a) You do not need a sign bit as all values are positive. b) You might want to consider something other than base 2 representation to eke out a little more precision given your data range (200.5). On the subject of extracting 'values' out of floating point representations I should like to suggest you track down a copy of the PowerPC Compiler Writers guide - this shows a process for injecting/extracting values by adding/subtracting a bias to move the values of interest to a more useful part of the representation and exclude the effects of the biased exponent. (The method may be described elsewhere - but this is one that I am familiar with and the guide is available on-line (or was). On the subject of range you may need to think very carefully about whether 200.5 is or isn't in your number range. It may be the case that when you look at alternative number bases it will become a moot point, but in your earlier examples you were running into a constraint problem because 200.5 was being rounded up to 201. (Read a bit more about rounding if you are unclear about this). Regards Stuart Chris L.