0

I've got a third party library that read values from laboratory scales. This library interface many scale models, each one with its own precision. What I need to do in my C++ application (that uses that library) is to read the weight value from the scale (double format) and print it, taking into account the scale precision (so giving the user, reading that value, the information about scale precision).

So, connecting a scale with 2 decimals precision, the output should be for example: 23.45 g Instead, connecting a scale with 4 decimals precision, the output should be for example: 23.4567 g

The fact is I don't know from the library the scale precision. The function looks like the following:

double value = scale.Weight();

If I just print the double value, the output could be in the form of:

1.345999999999999

instead of:

1.346

Is there a way to understand the double precision so that the output shows the weight with the scale precision?

EDIT: scale precision goes from 0 to 6 decimals.

ABCplus
  • 3,981
  • 3
  • 27
  • 43
  • ahhh... you need to get the precision out of the value?! sorry, i missunderstood your question... – user1810087 Mar 14 '17 at 16:30
  • @user1810087: correct, sorry for my explanation/English lang. – ABCplus Mar 14 '17 at 16:31
  • Please [edit] your question to make it clearer what it is that you really need to do - it's not obvious as it stands. – Paul R Mar 14 '17 at 16:32
  • @PaulR: I'm worried about adding more confusion if editing.. :-) can you edit it for me? – ABCplus Mar 14 '17 at 16:34
  • 1
    It is not possible to get the precision from a double(even if by human), how can you make sure `1.345999999999999` is not 15 precision? The library you use should give you that kind of information. – apple apple Mar 14 '17 at 16:36
  • @appleapple: ok, I understand. I've edit the question to cover this case: the maximum precision of a scale is 6 decimals. Does it make any difference? – ABCplus Mar 14 '17 at 16:38
  • @ABCplus If you know the precision, you can format the output as you want. But it is impossible to get the precision from a double. You need something else, maybe the library gives you the information, or you can ask the user to input it. – apple apple Mar 14 '17 at 16:42
  • @ABCplus If the maximum precision is 6, why don't you print just exactly this precision? If it is not necessary that the value is precise (and i assume it isn't) you could round at this precision (or at this +1), too. – anon Mar 14 '17 at 16:49

1 Answers1

1

No. This information should be inside scale class as double type has "fixed" precision and you cannot change it. Also type precision and printed precision are two different things. You can use type that has infinite precision but show always 2 digits after dot etc. If scale do not have precision information you could do a helper class and hard code precision inside it then correlate it with some scale property or type.

Logman
  • 4,031
  • 1
  • 23
  • 35