comp.lang.ada
 help / color / mirror / Atom feed
* Multiplying fixed-point by long integer
@ 2012-10-05  1:46 Adam Beneschan
  2012-10-05  2:12 ` Shark8
                   ` (4 more replies)
  0 siblings, 5 replies; 8+ messages in thread
From: Adam Beneschan @ 2012-10-05  1:46 UTC (permalink / raw)


I don't think I've ever seen this problem discussed before ...

4.5.5 defines predefined multiplication operators between any fixed-point type and Integer.  However, it isn't defined for any other integer type.  This can cause a problem if I have an integer type whose bounds are larger than Integer, since it obviously won't work to convert it to Integer.  Has anyone encountered this problem before, and how did you solve it?  I'm hesitant to try to solve it by converting things to floating point, since the mantissa of a floating-point may have fewer bits than the fixed-point or integer type, and fixed-point multiplication by an integer should be exact.

                                 -- thanks, Adam



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

* Re: Multiplying fixed-point by long integer
  2012-10-05  1:46 Multiplying fixed-point by long integer Adam Beneschan
@ 2012-10-05  2:12 ` Shark8
  2012-10-05  4:23 ` Jeffrey Carter
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: Shark8 @ 2012-10-05  2:12 UTC (permalink / raw)


You could use div & mod to break up the big integer into something Integer can handle. (Use mod to find an even divisor, and multiply that to the fixed-point div times... the restriction is it won't work on a number whose prime-factors are not contained in Positive'Range.)



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

* Re: Multiplying fixed-point by long integer
  2012-10-05  1:46 Multiplying fixed-point by long integer Adam Beneschan
  2012-10-05  2:12 ` Shark8
@ 2012-10-05  4:23 ` Jeffrey Carter
  2012-10-05  4:25 ` tmoran
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: Jeffrey Carter @ 2012-10-05  4:23 UTC (permalink / raw)


On 10/04/2012 06:46 PM, Adam Beneschan wrote:
>
> 4.5.5 defines predefined multiplication operators between any fixed-point
> type and Integer.  However, it isn't defined for any other integer type.
> This can cause a problem if I have an integer type whose bounds are larger
> than Integer, since it obviously won't work to convert it to Integer.  Has
> anyone encountered this problem before, and how did you solve it?  I'm
> hesitant to try to solve it by converting things to floating point, since the
> mantissa of a floating-point may have fewer bits than the fixed-point or
> integer type, and fixed-point multiplication by an integer should be exact.

Do the integer values fit in the range of the fixed-point type? If so, you could 
convert to the fixed-point type.

If not, then the fixed-point abs value must be < 1, and you're dividing the 
integer value by 1/'Small. That might give you some idea how to proceed.

-- 
Jeff Carter
"C++ is vast and dangerous, a sort of Mordor of
programming languages."
Jason R. Fruit
120

--- news://freenews.netfront.net/ - complaints: news@netfront.net ---



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

* Re: Multiplying fixed-point by long integer
  2012-10-05  1:46 Multiplying fixed-point by long integer Adam Beneschan
  2012-10-05  2:12 ` Shark8
  2012-10-05  4:23 ` Jeffrey Carter
@ 2012-10-05  4:25 ` tmoran
  2012-10-05 15:41   ` Adam Beneschan
  2012-10-05  5:14 ` anon
  2012-11-06 17:20 ` Jacob Sparre Andersen
  4 siblings, 1 reply; 8+ messages in thread
From: tmoran @ 2012-10-05  4:25 UTC (permalink / raw)


Create a 2nd fixed point type big enough to hold your long integer, and
multiply by that?



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

* Re: Multiplying fixed-point by long integer
  2012-10-05  1:46 Multiplying fixed-point by long integer Adam Beneschan
                   ` (2 preceding siblings ...)
  2012-10-05  4:25 ` tmoran
@ 2012-10-05  5:14 ` anon
  2012-11-06 17:20 ` Jacob Sparre Andersen
  4 siblings, 0 replies; 8+ messages in thread
From: anon @ 2012-10-05  5:14 UTC (permalink / raw)


An old discussion mostly bwtween the 1980s and 90s.

A file "farith.c" 

/*
 * multi-precision multiplication, division and exponentiation
 * for fixed point computations.
 */
 ...

which is a componet of "Ada/Ed" package that uses the "Multiple Precision 
Integer Arithetic Package" written by Robert B. K. Dewar in June 16, 1980. 
To preform some of the arithmetic calculations used in the "Ada/Ed" system.



Do a Google search for url of a ftp link to file using keywords

   multi-precision arithmetic "Robert B. K. Dewar" Ada/Ed farith.c


Other discussion on this subject can be found by just google and added 
the name Robert B. K. Dewar. Most will be between 1980 and 1999.


In <b06c0b56-3f3c-42e9-8b3a-76a4a27fc1cc@googlegroups.com>, Adam Beneschan <adam@irvine.com> writes:
>I don't think I've ever seen this problem discussed before ...
>
>4.5.5 defines predefined multiplication operators between any fixed-point t=
>ype and Integer.  However, it isn't defined for any other integer type.  Th=
>is can cause a problem if I have an integer type whose bounds are larger th=
>an Integer, since it obviously won't work to convert it to Integer.  Has an=
>yone encountered this problem before, and how did you solve it?  I'm hesita=
>nt to try to solve it by converting things to floating point, since the man=
>tissa of a floating-point may have fewer bits than the fixed-point or integ=
>er type, and fixed-point multiplication by an integer should be exact.
>
>                                 -- thanks, Adam




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

* Re: Multiplying fixed-point by long integer
  2012-10-05  4:25 ` tmoran
@ 2012-10-05 15:41   ` Adam Beneschan
  0 siblings, 0 replies; 8+ messages in thread
From: Adam Beneschan @ 2012-10-05 15:41 UTC (permalink / raw)


On Thursday, October 4, 2012 9:25:33 PM UTC-7, (unknown) wrote:
> Create a 2nd fixed point type big enough to hold your long integer, and
> multiply by that?

You know, I thought of that last night, after I had posted it.  But that raises another problem.  I assume you're thinking of something like this:

  type Long_Fixed is delta 1.0 range Long_Integer'First .. Long_Integer'Last;

  F1, F2 : Some_Other_Fixed_Type;

  F2 := F1 * Long_Fixed(N);

except that the Long_Fixed declaration isn't legal, because the range bounds have to be of a real type.  But how do you get the bounds to be real?

  type Long_Fixed is delta 1.0 range Long_Float(Long_Integer'First) ..
                                     Long_Float(Long_Integer'Last);

This isn't right, because it's possible that the largest floating-point type supported by the implementation has a mantissa size smaller than the number of bits in a Long_Integer (or, if you like, Long_Long_Integer or Interfaces.Integer_64), which means that the result won't be exactly right.

This seems like a flaw in the language, that it supports a "universal real" type  for static values that can be indefinitely precise, but there's no way to convert a static integer value to a universal real without going through a predefined floating-point type that may be less accurate than the integer.

Of course, the problem at hand could be solved with something like

  type Long_Fixed is delta 1.0 range -2.0**(Long_Integer'Size - 1) ..
                                     2.0**(Long_Integer'Size - 1) - 1.0;

Seems a bit hokey that you have to go through this, though.

                         -- Adam



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

* Re: Multiplying fixed-point by long integer
  2012-10-05  1:46 Multiplying fixed-point by long integer Adam Beneschan
                   ` (3 preceding siblings ...)
  2012-10-05  5:14 ` anon
@ 2012-11-06 17:20 ` Jacob Sparre Andersen
  2012-11-06 20:33   ` Adam Beneschan
  4 siblings, 1 reply; 8+ messages in thread
From: Jacob Sparre Andersen @ 2012-11-06 17:20 UTC (permalink / raw)


Adam Beneschan <adam@irvine.com> writes:

> 4.5.5 defines predefined multiplication operators between any
> fixed-point type and Integer.  However, it isn't defined for any other
> integer type.

Is there a reason for that?  (I can't find anything in the AARM.)

Greetings,

Jacob
-- 
"Science is like sex: sometimes something useful comes out,
 but that is not the reason we are doing it"
                                          -- Richard Feynman



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

* Re: Multiplying fixed-point by long integer
  2012-11-06 17:20 ` Jacob Sparre Andersen
@ 2012-11-06 20:33   ` Adam Beneschan
  0 siblings, 0 replies; 8+ messages in thread
From: Adam Beneschan @ 2012-11-06 20:33 UTC (permalink / raw)


On Tuesday, November 6, 2012 9:20:55 AM UTC-8, Jacob Sparre Andersen wrote:
> Adam Beneschan writes: 
> 
> > 4.5.5 defines predefined multiplication operators between any
> > fixed-point type and Integer.  However, it isn't defined for any other
> > integer type.
> 
> Is there a reason for that?  (I can't find anything in the AARM.)

Having it predefined for more than one integer type would lead to ambiguities if you gave it an integer literal, which is probably one of the most common cases.  That is, if F is an object of a fixed-point type, something like

  F := F * 3;

won't compile if "*" were defined multiple times for a fixed-point type and more than one integer type.

The same issue arises with the "**" operator which is predefined only for an Integer right operand.  That shouldn't be a real problem, however, since if N is a value that will fit in a Long_Integer but not an Integer, you're not going to be able to usefully raise anything to the Nth power anyway.

I proposed a suggested solution to Ada-Comment that involved a new language-defined generic, since I didn't want to add to the mess involving predefined fixed-point multiplication and name resolution rules (there have already been some issues with that that probably caused the ARG some headaches).  Tucker pointed out to me that there was a way to get it done without involving predefined floating-point types:

     type Long_Fixed is delta 1.0 range
        Long_Integer'Pos(Long_Integer'First) * 1.0 ..
          Long_Integer'Pos(Long_Integer'Last) * 1.0;

and then use predefined "*" between two fixed-point types, converting the Long_Integer to Long_Fixed.  Seems like a roundabout way of doing things, but probably acceptable given that the need for this is probably pretty uncommon.

                            -- Adam



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

end of thread, other threads:[~2012-11-08  5:40 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-05  1:46 Multiplying fixed-point by long integer Adam Beneschan
2012-10-05  2:12 ` Shark8
2012-10-05  4:23 ` Jeffrey Carter
2012-10-05  4:25 ` tmoran
2012-10-05 15:41   ` Adam Beneschan
2012-10-05  5:14 ` anon
2012-11-06 17:20 ` Jacob Sparre Andersen
2012-11-06 20:33   ` Adam Beneschan

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