Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

We have a setup where a remote C++ client invokes an Oracle function (using OCCI library) passing some data. This Oracle function then inserts the data into a table. One of the data which gets passed to the function is AMOUNT, which is a floating point number.

In C++ client, this AMOUNT variable is defined as a "double". Both the Oracle function and the table define the data type for this column as "NUMBER" (without any precision or scale defined explicitly).

We are seeing a lot of inconsistency between what the client is sending and what we are receiving and storing at our database. For example, the client claim that they are passing a value “35.6”, but what we are seeing in our table is “35.59999847”.

We are supposed to store the values with an accuracy of up to 6 decimal places. If I change the data type definition of the AMOUNT column from NUMBER to NUMBER(38,6), I get “35.599998”. If I change it to NUMBER (38,5), I end up getting “35.6”.

Can someone suggest what might be happening here? I know that it is not advisable to treat the amount or price values as floating point numbers and should use whole numbers instead, but in this case we don’t have any control over the C++ client. And we are seeing this issue for a very large set of data (more than 50% of the data suffers this issue).

Also, this issue is not just limited to floating point numbers. We can see inconsistencies even with large whole numbers (for example, a value passed as 1000000000 gets stored as 1000000004).

share|improve this question
possible duplicate of Floating point error in representation? – Oli Charlesworth 1 hour ago
If the C++ client uses DECIMAL and th database uses NUMBER, where exactly does floating-point come into it? – EJP 50 mins ago
@EJP - sorry, meant to say 'double' and not 'decimal'. Edited my post. – cdoe 27 mins ago
@OliCharlesworth, yes I have seen that site before. But I never thought that the issue would be too widespread. Also, the same C++ client is making calls to other systems as well (MySQL database, for example). And they are in sync. So far this issue is seen only with C++ and Oracle integration. – cdoe 12 mins ago

1 Answer

You may not have control over the client but can you change the interface?

Passing a floating point to Oracle will inevitable cause this problem, because they are an inherently imprecise datatype. But if you can amend the interface you could pass two integers to Oracle: the AMOUNT's integer and the AMOUNT's fractional part. Oracle can then combine these two integers to make a single NUMBER variable which it can use happily.

share|improve this answer
1  
But it's already too late. The OP stated that the client stores the value in a double, so the precision has already been lost. – Oli Charlesworth 27 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.