Topic: [ratio] Assignation between equivalent ratios


Author: viboes <vicente.botet@wanadoo.fr>
Date: Mon, 7 Dec 2009 00:35:46 CST
Raw View
Hi,

I was wondering if we need to be able to make assignations between
equivalent ratios. e.g.

ratio<1,3> r1;

ratio<3,9> r2;

r1 = r2; // (1)


Even if they are equivalents (synonym ) as its num and den are the
same, there is no defined assignation in N3000. So (1) should not
compile with the current recommendation.

Other example

ratio<1,3> r1;

ratio<2,3> r2;

r1 = r2-r1;  // the type of this expression could be ratio<3,9> so the
compilation fails.

IMO, this should compile, and so we need to add a specific assignment
operator.

Otherwise the recommendation should clarify the meaning of synonym on
the ratio arithmetic operators

[ratio.arithmetic] 3

"The nested typedef type shall be a synonym for ratio<T1, T2> where T1
has the value R1::num * R2::den - R2::num * R1::den and T2 has the
value R1::den * R2::den."

Maybe we should talk of normalized ratio and not of synonym.  BTW,
where is synonym defined?

In addition I think that we need also to add that the declaration of
no normalized ratios, as ratio<3,9>, should fail at compile time as if
we allows them we can not assign any arithmetic expression if
arithmetic operator can normalize normalize.

I'm missing something? Any thoughts?


Best regards
_____________________
Vicente Juan Botet Escrib


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Anthony Williams <anthony.ajw@gmail.com>
Date: Mon, 7 Dec 2009 04:15:33 CST
Raw View
viboes <vicente.botet@wanadoo.fr> writes:

> I was wondering if we need to be able to make assignations between
> equivalent ratios. e.g.
>
> ratio<1,3> r1;
>
> ratio<3,9> r2;
>
> r1 = r2; // (1)
>
>
> Even if they are equivalents (synonym ) as its num and den are the
> same, there is no defined assignation in N3000. So (1) should not
> compile with the current recommendation.

True, but that doesn't matter. std::ratio does not hold any values, so
runtime assignments and so forth don't make any sense. Either the values
are equivalent, so assignment is unnecessary, or they are not, so
assignment can't work.

> Other example
>
> ratio<1,3> r1;
>
> ratio<2,3> r2;
>
> r1 = r2-r1;  // the type of this expression could be ratio<3,9> so the
> compilation fails.

Runtime subtraction using operator- is not defined for std::ratio. It is
for compile-time arithmetic only. Ratio subtraction is defined with
std::ratio_sub<R1,R2>::type. The result is defined to be over the lowest
common denominator, so ratio_sub<ratio<2,3>,ratio<1,3>>::type is
ratio<1,3>

> Otherwise the recommendation should clarify the meaning of synonym on
> the ratio arithmetic operators
>
> [ratio.arithmetic] 3
>
> "The nested typedef type shall be a synonym for ratio<T1, T2> where T1
> has the value R1::num * R2::den - R2::num * R1::den and T2 has the
> value R1::den * R2::den."
>
> Maybe we should talk of normalized ratio and not of synonym.  BTW,
> where is synonym defined?

Synonym takes on its usual meaning: a word that means the same as
another word. Thus using ratio_sub<ratio<2,3>,ratio<1,3>>::type "means
the same" as using ratio<1,3> i.e. it is the same type.

> In addition I think that we need also to add that the declaration of
> no normalized ratios, as ratio<3,9>, should fail at compile time as if
> we allows them we can not assign any arithmetic expression if
> arithmetic operator can normalize normalize.

There is no runtime assignment or operations, and all compile-time
arithmetic is defined to take care of denormalized ratios, so this is
not a problem.

Anthony
--
Author of C++ Concurrency in Action     http://www.stdthread.co.uk/book/
just::thread C++0x thread library             http://www.stdthread.co.uk
Just Software Solutions Ltd       http://www.justsoftwaresolutions.co.uk
15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]