floating point precision and type casting

Aug 2, 2016 at 6:42am
Hi

I want to make sure that I have the greatest precision when doing multiplication, so I try to use double type.

afirstx, alastx and apointx are of type int.
factor is of type double.

Please tell me what is the most appropriate code, from the two versions:

version 1:
1
2
3
double vfirstx =  afirstx * factor;
double vlastx =  alastx * factor;
double vpointx =  apointx * factor;


version 2:
1
2
3
double vfirstx = ((double) afirstx) * factor;
double vlastx = ((double) alastx) * factor;
double vpointx = ((double) apointx) * factor;



Is version 1 enough, or I have to resort to version 2?
Aug 2, 2016 at 7:57am
C++ will do coercion and promote int to double so version 1 should suffice, though I dont think using version 2 will do any harm
Aug 2, 2016 at 8:06am
Hi,

Just about casting, in the situation where you do it to avoid a warning about narrowing of a type (the opposite of your question), use static_cast<type>(value) rather than a C style cast.

double is not the greatest precision for FP, although 15 or 16sf with double is normally enough for most things. long double can do 18 or 19sf. Libraries exist for arbitrary precision types.

Hope this helps a little :+)
Aug 2, 2016 at 8:19am
> Hope this helps a little :+)
Your explanation does not help, unfortunately.
Aug 2, 2016 at 8:32am
@closed account 5a8Ym39o6

Ok, I will bite, this once: So you continue to troll, this time with petulance.
Aug 2, 2016 at 8:33am
Holy heaven, what did I ever do to deserve this kind of treatment? O.o
Aug 2, 2016 at 8:38am
> Ok, I will bite, this once : So you continue to troll, this time with petulance.
Please remember that I at least have 1/3 valueable posts in total (389.3333 posts)
Aug 2, 2016 at 8:39am
Holy heaven, what did I ever do to deserve this kind of treatment?

Filling the forums with nonsense and misleading posts?
Aug 2, 2016 at 8:41am
> Filling the forums with nonsense and misleading posts?
That is not what I intended to do. Someone is actually tricking me from behind.
Aug 2, 2016 at 12:54pm
@codewalker

The operation might give an inaccurate result if I would store it in a float number (according to IEEE floating point arithmetic). Maybe because variable "factor" is a double, then the result of it is automatically stored in a double memory place?
Aug 3, 2016 at 2:05am
The operation might give an inaccurate result if I would store it in a float number (according to IEEE floating point arithmetic).


Yes. float can only do 6 or 7 sf, so it's precision is easily exceeded.

Maybe because variable "factor" is a double, then the result of it is automatically stored in a double memory place?


Because factor is a double means the type of the expression on the rhs of = is promoted to double. vfirstx is also double, and that is it's storage type (the lhs of =). But people often make the mistake of having an expression on the rhs that is double, then cause an implicit cast to some lesser type (float or some int type say) on the lhs. This should attract a warning if one has a compiler flag turned on to do it.

http://en.cppreference.com/w/cpp/language/implicit_conversion

Some extra compiler warnings for g++:
http://www.cplusplus.com/forum/general/183731/#msg899203
Topic archived. No new replies allowed.