New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simple mathematics gone wrong [CORE2849] #3235
Comments
Commented by: @dyemanov The rules for numerics are that the result of NUMERIC(A, X) / NUMERIC(B, Y) has the scale of exactly X + Y. |
Commented by: @hvlad Can't resist : Many users never read documentation. |
Commented by: @helebor The SQL engine delivers just what you ask for. This is a support question, not a bug report. Post your examples to the firebird-support list and ask why your expressions are not getting the results you expect. Read up about integer division, scale, precision and numeric types in standard SQL and you will start to understand why SQL arithmetic doesn't work like your pocket calculator. Hint: Supply enough places of decimal in your operands to provide the scale you want in your result and to avoid being caught by integer division. (In SQL, 10/3 returns 3, which is correct. 5/3 returns 1, which is also correct.) Try Please don't post bug reports until it is confirmed, via firebird-support first, and then in firebird-devel, that a bug exists. |
Commented by: Hilmar Brodner (syn) Sorry, but in my eyes it's still a bug: Doing a SELECT with absolute values and getting different results depending on the way the calculation is written does not sound right to me. Maybe I'm expecting too much, but why does FB convert 13.3 to a NUMERIC(x.1) and not a double precision? As for the documentation: |
Commented by: @dyemanov 13.3 is a NUMERIC(x, 1) literal because the SQL specification declares it being so. 13.3e0 is a double precision literal though. |
Commented by: @hvlad Hilmar, read IB6 beta docs, Language Reference (available at http://www.ibphoenix.com/downloads/60LangRef.zip) page 22 - Multiplication and Division |
Modified by: @pcisarstatus: Resolved [ 5 ] => Closed [ 6 ] |
we want to migrate Fb25 to Fb4 this code below bad calculation. execute block begin -- calculate vat amount from gross amount and tax percent -- expected vat amount 8074.190476; -- expected 8074.190476 -- expected 0,047619 -- expected 0,0476 end |
It is scaled integer but still integer.
|
@kollerpal ,use FB40 with dialect 1, not with dialect 3. |
Or use double precision literals (or casts to double precision), to force the calculation itself to happen in double precision. |
Suggesting dialect 1 is not good as 1) it has been deprecated since 2000 (if you read the IB 6.0 docs, it was only intended to exist for one version), and 2) Firebird 4.0 has formally announced it deprecated in the release notes, announcing future removal. |
this scale is wrong
scale to if í casting cast(1/3 as integer)
declare variable result integer; result = 1/3; scale auto maybe but
compiler warning should be
thanks
Dimitry Sibiryakov ***@***.***> ezt írta (időpont: 2022.
szept. 29., Cs, 14:36):
… kollerpal wrote 29.09.2022 14:34:
> I will note it 1/3 result is 0 or 5/3 result is 1 is not good result.
compilers
> minimum sign warning error
> if result var as integer then good result but this sample result type
not integer!!
It is scaled integer but still integer.
--
WBR, SD.
—
Reply to this email directly, view it on GitHub
<#3235 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AKNMSH2VKYYVQWM6FOKFUULWAWENPANCNFSM6AAAAAAQYXIFPU>
.
You are receiving this because you commented.Message ID:
***@***.***>
--
Koller Pál
|
Scale cannot be "right" or "wrong", it is just a number of digits which are
separated by decimal dot. So your 3.0 is 30 with scale -1.
|
declare variable idiv integer;
declare variable num_div numeric(14,4);
...
idiv = 6/7 = 0 maybe but compiler warning required.
num_div = 6/7 = 0 is unacceptable independent of fixed or floating point
aritemtic.
the cast( 6e0/7e0 as numeric(14,5)) working fine but very hard migration
proble for application
Dimitry Sibiryakov ***@***.***> ezt írta (időpont: 2022. okt.
11., K, 11:24):
… Scale cannot be "right" or "wrong", it is just a number of digits which
are
separated by decimal dot. So your 3.0 is 30 with scale -1.
—
Reply to this email directly, view it on GitHub
<#3235 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AKNMSH7YNQ26LJIDYZAK443WCUW4RANCNFSM6AAAAAAQYXIFPU>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
--
Koller Pál
|
I'm locking this conversation as it is becoming repetitive. For support questions, please go to https://groups.google.com/g/firebird-support. |
Submitted by: Hilmar Brodner (syn)
We have stumbled across this on a regular basis. Strangely enough I haven't found a bug report on this here although this should be a rather common and quite critical problem:
SELECT -- aim is to get 13,3% of 113 = 15,02900
-- With CAST
CAST((113 * (13.3 / 100)) AS NUMERIC(15,5)) AS "Wrong_1",
CAST((113 * (13.3 / 100.0)) AS NUMERIC(15,5)) AS "Wrong_2",
CAST((113 * 13.3 / 100) AS NUMERIC(15,5)) AS "Wrong_3",
FROM RDB$DATABASE
The problem basically results from FB not adding the necessary amount of decimal digits, thus continuing to calculate with the initial amount, although the division should have added further digits.
The text was updated successfully, but these errors were encountered: