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 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,ASCII-7-bit Path: g2news2.google.com!postnews.google.com!e1g2000pra.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: How to put 200 into an integer sub-type of 16 bits (code included) Date: Wed, 14 Jan 2009 14:47:04 -0800 (PST) Organization: http://groups.google.com Message-ID: <262da306-0c88-4feb-a067-8966dab23589@e1g2000pra.googlegroups.com> 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> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1231973224 1695 127.0.0.1 (14 Jan 2009 22:47:04 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 14 Jan 2009 22:47:04 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: e1g2000pra.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.7.12-1.3.1,gzip(gfe),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:4275 Date: 2009-01-14T14:47:04-08:00 List-Id: On Jan 14, 1:23 pm, ChristopherL wrote: > On Jan 14, 12:41 pm, Adam Beneschan wrote: > > > > > On Jan 14, 12:13 pm, ChristopherL wrote: > > > > I want to copy bits from an Ada float to a user defined short integer > > > maintaining the same > > > bit representation. > > > OK, why? Depending on your hardware, a float is likely 32 or 64 bits, > > maybe 80. I've occasionally seen different sizes, but I don't recall > > ever seeing a 16-bit float, and definitely not an 8-bit float. > > > Your short integer is an 8-bit integer. (If you want it to be a 16- > > bit integer, why would you give it a range of -128 .. 127, rather than > > -32768 .. 32767?) > > > OK, assuming that want to copy bits from an "Ada float", which is > > actually a hardware float supported by your processor and has nothing > > to do with your programming language, to a short integer: You want to > > copy bits from a 32-bit float, perhaps, to an 8-bit integer. Which > > bits do you want to copy? The ones containing the exponent or part of > > it, or the low-order mantissa bits, or some other sequence of bits? > > Why don't you want to copy the other 24 bits? > > > If you have no idea what I'm talking about, then you have certainly > > stated your problem incorrectly, and you need to rethink your problem > > and restate what you want to accomplish, at a high level, without > > referring to "bits". Actually, I already suspect that you don't want > > what you said you want (copying bits from a float), because if you > > really did want to copy bits from a floating-point representation, you > > wouldn't be concerned with rounding. > > > -- Adam > > Please remove the rounding operation for now from this discussion. > > This problem was given to me to try to solve who said it's probably > impossible! > > 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, mathematical PI , 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? It's pretty clear that you're very confused about something. Earlier you wanted to store a bit representation; now you want to store a number. Not the same thing. A number is a mathematical concept, and a bit representation is a sequence of 1's and 0's. If you want an 8-bit short integer to hold the value 200, then declare it with a range 0..255, and assign 200 into it: type Short_Integer is range 0 .. (2**8) - 1; -- same as 0..255 Result : Short_Integer; Result := 200; --or something like-- Result := Short_Integer (Arg + 0.5); --this is a type conversion, not an Unchecked_Conversion Don't declare Short_Integer as -(2**7) .. (2**7)-1. 2**7 is 128, so that gives you a range of -128 .. 127, and 200 is not in this range. That should be obvious, but perhaps you're used to programming languages that don't work in the obvious way. Ada isn't like that. If you tell it that a number's type is in the range -128 .. 127, then it only allows numbers in the range -128 .. 127 to be put in there. Too simple? Note also that the bit representation, 11001000, is *not* a sequence of bits that exists anywhere in the 32-bit floating-point representation of 200.0. So if you still think you want to copy *bits* from a float, or from the representation of a floating-point number, "maintaining the same representation" as you said earlier, you're very confused. You can't maintain the representation of a 32- bit float in an 8-bit integer. There aren't enough bits. -- Adam