Closed ka-ba closed 8 months ago
I am so sorry that it took me so long to come back to you. I was working hard on the V2 version of the engine, and only now is it ready enough to actually show you how to solve your issue in this library.
Here it is https://godbolt.org/z/vMcdxzEY5. When we are dealing with custom representation types, we need to describe their semantics with:
treat_as_floating_point
to specify when it has floating-point-like semanticsis_scalar
(new in V2) to specify that it describes the representation type of scalar quantities.Moreover, for your example to compile, you also needed a converting constructor from one type to another (radian_rep_t -> degree_rep_t, degree_rep_t -> radian_rep_t) so the underlying library framework knows how to initialize them correctly from the function arguments.
Also, as now they are floating-point-like you do not need any explicit casts anymore to convert between those.
Sorry again for the late answer. Please let me know if I can close the issue or if there are some other issues that we need to resolve first.
Thanks a lot for your answer! I'm currently working on another branch (and just in spare time). I'll try to have a look at the above solution soon. It's just a small project, but it would be nice to use the units lib and have all type interaction checked at compile time.
Sure, please let me know if your problem is fixed, and we can close this issue.
Sorry for the delay - had quite a hard time converting anything from 0.7-syntax to v2-syntax (even more so since the IDE uses clangd for problem highlighting and clang seems to have known bug(s) concerning concepts :frowning_face: ). Everything compiles fine (using gcc), but I didn't do a lot of testing. As for angular measures: the two quantities compile well, especially the conversion between them - as per your example. Thanks again for that! But to me it seems that the resulting value is incorrect!? Having an angle of PI rad, it should print something around 180°, shouldn't it? (c.f. your godbolt-link above) I can't see a respective bug in the base classes. Do you have an idea here as well?
(NB: In case you are interested, the commit with the changes from v0.7 -> v2.0 : https://gitlab.com/ka-ba/vfrp/-/commit/b4a5bac06b50faec4eb7050820e5f4a8b41bdb81 , sorry that renaming one type clutters the diffs)
@ka-ba, thanks for sharing your code. I left some comments in your commit.
Regarding your question, the problem is that your types are implicitly convertible from double
, which results in radian_base_t
being selected as the type used to do all the internal conversions in the library. They are also implicitly convertible to each other, which does not help as well. I am not sure why you need the other constructor. If it is needed, it should be explicit as well. If not, just remove it. Also, custom overloads of arithmetic operators seem to not be needed as the implicit conversion to double
will provide them anyway.
With the above changes the code works as expected: https://godbolt.org/z/Y443b7xec.
@ka-ba, please let us know if the above addresses your problems and if we can close this issue.
@mpusz Thanks again for your help with the library!
The arithmetic operators and the implicit constructor from double
also had other usages in my code. As implicit construction causes severe problems, I have to do without, of course. I found work-arounds (though not very nice looking).
It doesn't seem necessary to reach for clumsy work-arounds concerning arithmetic, so I'll keep the operators with the base_classes.
If of interest for you, pls see https://godbolt.org/z/5c8rdKd7W for details.
The original problem is solved, I'd say - the issue should be closed. Thanks again for your help and for the library as such! I hope it's going to be used in many projects.
P.S.: Also thanks a lot for commenting my project. Taking care of your comments will be my next task.
This is my problem, first described in a discussion thread, formulated as proper issue, like proposed in https://github.com/mpusz/units/discussions/428#discussioncomment-5470934 .
I created two types that represent angles with a custom representation base class each (for the different units radian and degree). While being able to
quantity_cast
one into the other in both directions, the code fails toquantiy_cast
rad into rad or degree into degree. (Pls. note that the problem arises with a function that accepts any angle type, but internally converts to radian before starting computations (which is my implementation ofsin
, also to be found below).)Pls find a down-sized example of the problem here: https://godbolt.org/z/c58P97hzx (the complete code also follows at the end of this text for reference) (The compiler is set to gcc11.3 as that is my local version, but the recent versions of gcc and clang complain just alike.)
As
quantity_cast
ing otherunits
types to themselves works AFAI can see, there seems to be a problem with my use of the library. At this stage I won't suspect an issue within the library, but any help with it's proper usage is very welcome.The example code that can also be found in compiler explorer (link above) (
sin
provided for the sake of completeness):(EDIT: Just realised that the error message is missing here...) The error message that g++ produces on compiler explorer: