1

I'm just trying to do something. There is a some computation to be done using double-type variables.

We have two parameters "x" and "y" where we need to store log2(x) with y-bit precision.

First thing I need to is

double a = std::log2(x); // x is an input given by a problem

Then, we truncate "a" by just keeping "y"-bit of information after decimal points.

How can we do this?

user9414424
  • 498
  • 3
  • 15
  • Not sure I fully understand the question, do you want for example a 24bit or 13bit double? – David Ledger Jun 19 '19 at 03:08
  • If that is what you want, look here: https://www.boost.org/doc/libs/1_59_0/libs/numeric/odeint/doc/html/boost_numeric_odeint/tutorial/using_arbitrary_precision_floating_point_types.html – David Ledger Jun 19 '19 at 03:16
  • Do you need to do computations with a value truncated to a certain number of decimal digits or do you just need to print the value out? This sounds a bit like an [XY problem](https://meta.stackexchange.com/q/66377). – eesiraed Jun 19 '19 at 04:56
  • See [this question](https://stackoverflow.com/questions/53596950/platform-independent-way-to-reduce-precision-of-floating-point-constant-values/53598561#53598561) and [this question](https://stackoverflow.com/questions/14285492/efficient-way-to-round-double-precision-numbers-to-a-lower-precision-given-in-nu/14285800#14285800), presuming you want to round, not truncate. This is essentially a duplicate except for the language. The C question should suffice for this one with `float32_t` changed to `double`. – Eric Postpischil Jun 19 '19 at 10:44
  • Possible duplicate of [platform independent way to reduce precision of floating point constant values](https://stackoverflow.com/questions/53596950/platform-independent-way-to-reduce-precision-of-floating-point-constant-values) – L. F. Jun 21 '19 at 10:16

0 Answers0