-2

For instance, I have a floating point number 0.02344489282. I want to be able to make sure that every float that I have is upto two decimal points: 0.02. It will be inexact, I'm sure but the entire floats in my code should be able to truncate anything after two decimal places. I have seen other related posts on Stack Overflow but they deal with outputting the decimal to two points.

Goal: to optimize memory consumption at the expense of accuracy. But the accuracy can be downgraded to 5-15%.

Practical example: I am executing a Kalman filter. Instead of exact values of noise and actual values, I try to find the approximate values by shortening the bit width of variables. Then I'll find the difference of accuracy of the former script and the latter script and how much of energy and memory is saved.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
pink puffles
  • 43
  • 1
  • 8
  • Is this related to printing the numbers? What do you mean by "every float that I have"? – DeiDei Jun 02 '18 at 20:58
  • 4
    Use an integer and divide by 100.0 – Ken White Jun 02 '18 at 20:59
  • @DeiDei Every float that is every inputted or every declared. All the floats in the program – pink puffles Jun 02 '18 at 21:03
  • 1
    Floating point numbers in general cannot be truncated to a certain number of decimal places. You should either use some other data type, or reconsider your idea. – n. m. could be an AI Jun 02 '18 at 21:06
  • 1
    If you do not manage to represent all your numbers in integers (which are handled like being 1/100 worth), then you will encounter many problems related to https://stackoverflow.com/questions/588004/is-floating-point-math-broken – Yunnosch Jun 02 '18 at 21:06
  • 1
    What you are looking for is fixed point arithmetic. Floating point number _by nature_ cannot be truncated this way. In general, the idea of storing numbers as integers multiplied by 100 is a simple fixed point representation. – joe_chip Jun 02 '18 at 21:10
  • @Yunnosch is there any way that I reduce the bits from 64 to let's say 20. – pink puffles Jun 02 '18 at 21:10
  • Yes, use an integer, handle it as if it had only 1/100 value. – Yunnosch Jun 02 '18 at 21:12
  • What do you **really** want to achieve? Precise calculation with two decimal places or optimise the memory consumption? – Yunnosch Jun 02 '18 at 21:14
  • @Yunnosch How is an int represented. Can you give me some link for more detail on ints? – pink puffles Jun 02 '18 at 21:14
  • @Yunnosch optimize the memory consumption at the expense of accuracy. – pink puffles Jun 02 '18 at 21:15
  • Then please explain that in your question. Explain which values you need to represent and how terse you want the memeory optimisation. I think you need to completely rewrite your question. So much so, that the new question is not answered by the answers below. So it would be fairer to make a new question. Also an interesting read for you: https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – Yunnosch Jun 02 '18 at 21:15
  • 1
    Restricting the significance of working values is a very bad idea. Always work to the best significance available, and shorten the output for humans if required. If the goal is reduce memory use - it won't. – Weather Vane Jun 02 '18 at 21:20

1 Answers1

1

Two possible solutions:

  1. Use integers representing units of 1/100.
  2. Use floating point, but only use integer multiples of 0.25 (i.e. numbers ending in .25, .50, .75, or .00) since these are the only floats which have only two decimal places.

Since option 2 is almost certainly not what you actually want, go for 1.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • I did, but I'm not sure I want to spend any more effort chasing changing questions with XY problems... – R.. GitHub STOP HELPING ICE Jun 02 '18 at 21:30
  • Of course. I just wanted to give you a heads up. Actually I am totally with you. But I would probably try to protect myself from downvoters which do not see that the answer was, at some time, correct. Probably not so important once the 100k is reached... ;-) – Yunnosch Jun 02 '18 at 21:33