K
K
Kostya Bakay2016-11-19 20:01:24
Java
Kostya Bakay, 2016-11-19 20:01:24

How to implement rounding numbers in Java by digits?

This is a signal processing task. There is a mathematical function (in my case, sin will be taken from a sequence of fractions with x). I need to do 8 and 16 bit rounding of the operands (and eventually the result) when evaluating the function and the result for each x should be three different function values: normal, with 8 bit rounding and with 16 bit rounding. As I understand it, in Java, to round up to 8 digits, it is enough to convert a number to byte, and if up to 16 digits, then to short, but the problem is that I have fractional function values ​​and, as I understand it, this cannot be done so simply. I came to the conclusion that it is possible to separate the integer and fractional parts and round the fractional part, perceiving it as a whole according to the already mentioned algorithm with byte / short, and then glue it back. How can I do that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Mirilaczvili, 2016-11-26
@kostyabakay

In a rational number, for accuracy, you can make the mantissa wider. Or, to increase the range of numbers - an integer part. For storage of currency use BigDecimal.
Here are some materials in English. Hope it helps:
Floating-point representation
Survey of Floating-Point Formats
https://web.archive.org/web/20110831135510/http://...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question