Re: Precision and Scale innacuracies when dividing
From: Bri Gipson (spammers_at_not.welcome.com)
Date: 08/24/04
- Next message: Jonas Larsen: "Instead of trigger!"
- Previous message: David G.: "Re: Removing Old/Unused Stored Procs...."
- In reply to: Bri Gipson: "Precision and Scale innacuracies when dividing"
- Next in thread: Steve Kass: "Re: Precision and Scale innacuracies when dividing"
- Reply: Steve Kass: "Re: Precision and Scale innacuracies when dividing"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 24 Aug 2004 03:56:11 GMT
Here's perhaps a better example. It shows that reducing the precision
produces a more defined result. Notice how using a DECIMAL(30,25) produces a
result defined to the 23rd decimal place while using a DECIMAL(38,25) only
produces a result to the 15th decimal place.
Still the result is way off of the real value, as mentioned in the prior
post.
-Bri Gipson
-- Code
CREATE TABLE #values
(
dividend DECIMAL(20,10),
divisor DECIMAL(20,10)
)
CREATE TABLE #bad_precision_a
(
set_name CHAR(1) DEFAULT 'A',
quotient1 DECIMAL(33,15),
quotient2 DECIMAL(38,25),
quotient3 DECIMAL(26,25),
quotient4 DECIMAL(25,20)
)
CREATE TABLE #bad_precision_b
(
set_name CHAR(1) DEFAULT 'B',
quotient1 DECIMAL(33,15),
quotient2 DECIMAL(38,25),
quotient3 DECIMAL(26,25),
quotient4 DECIMAL(25,20)
)
CREATE TABLE #bad_precision_c
(
set_name CHAR(1) DEFAULT 'C',
quotient1 DECIMAL(33,15),
quotient2 DECIMAL(38,25),
quotient3 DECIMAL(26,25),
quotient4 DECIMAL(25,20)
)
CREATE TABLE #bad_precision_d
(
set_name CHAR(1) DEFAULT 'D',
quotient1 DECIMAL(33,15),
quotient2 DECIMAL(38,25),
quotient3 DECIMAL(26,25),
quotient4 DECIMAL(25,20)
)
CREATE TABLE #bad_precision_e
(
set_name CHAR(1) DEFAULT 'E',
quotient1 DECIMAL(33,15),
quotient2 DECIMAL(38,25),
quotient3 DECIMAL(26,25),
quotient4 DECIMAL(25,20)
)
-- dividend / divisor = quotient
/*
INSERT INTO #values ( dividend, divisor )
VALUES ( 0.0021, 0.0315 )
INSERT INTO #values ( dividend, divisor )
VALUES ( 0.000000073, 0.00042 )
*/
INSERT INTO #values ( dividend, divisor )
VALUES ( 0.0000000129381952, 90328.15713241413243 )
-- The quotient should be: 0.00000000000014323546068844956882855010339423
INSERT INTO #bad_precision_a
( quotient1, quotient2, quotient3, quotient4 )
SELECT
dividend / divisor,
dividend / divisor,
dividend / divisor,
dividend / divisor
FROM
#values
INSERT INTO #bad_precision_b
( quotient1, quotient2, quotient3, quotient4 )
SELECT
CONVERT( DECIMAL(38,25), dividend ) / CONVERT( DECIMAL(38,25), divisor ),
CONVERT( DECIMAL(38,25), dividend ) / CONVERT( DECIMAL(38,25), divisor ),
CONVERT( DECIMAL(38,25), dividend ) / CONVERT( DECIMAL(38,25), divisor ),
CONVERT( DECIMAL(38,25), dividend ) / CONVERT( DECIMAL(38,25), divisor )
FROM
#values
INSERT INTO #bad_precision_c
( quotient1, quotient2, quotient3, quotient4 )
SELECT
CONVERT( DECIMAL(38,25), dividend ) / divisor,
CONVERT( DECIMAL(38,25), dividend ) / divisor,
CONVERT( DECIMAL(38,25), dividend ) / divisor,
CONVERT( DECIMAL(38,25), dividend ) / divisor
FROM
#values
INSERT INTO #bad_precision_d
( quotient1, quotient2, quotient3, quotient4 )
SELECT
CONVERT( DECIMAL(30,25), dividend ) / CONVERT( DECIMAL(30,25), divisor ),
CONVERT( DECIMAL(30,25), dividend ) / CONVERT( DECIMAL(30,25), divisor ),
CONVERT( DECIMAL(30,25), dividend ) / CONVERT( DECIMAL(30,25), divisor ),
CONVERT( DECIMAL(30,25), dividend ) / CONVERT( DECIMAL(30,25), divisor )
FROM
#values
INSERT INTO #bad_precision_e
( quotient1, quotient2, quotient3, quotient4 )
SELECT
CONVERT( DECIMAL(30,25), dividend ) / divisor, -- 18,17
CONVERT( DECIMAL(30,25), dividend ) / divisor, -- 18,17
CONVERT( DECIMAL(30,25), dividend ) / divisor, -- 18,17
CONVERT( DECIMAL(30,25), dividend ) / divisor -- 18,17
FROM
#values
SELECT * FROM #values
SELECT * FROM #bad_precision_a
UNION
SELECT * FROM #bad_precision_b
UNION
SELECT * FROM #bad_precision_c
UNION
SELECT * FROM #bad_precision_d
UNION
SELECT * FROM #bad_precision_e
DROP TABLE #bad_precision_a
DROP TABLE #bad_precision_b
DROP TABLE #bad_precision_c
DROP TABLE #bad_precision_d
DROP TABLE #bad_precision_e
DROP TABLE #values
- Next message: Jonas Larsen: "Instead of trigger!"
- Previous message: David G.: "Re: Removing Old/Unused Stored Procs...."
- In reply to: Bri Gipson: "Precision and Scale innacuracies when dividing"
- Next in thread: Steve Kass: "Re: Precision and Scale innacuracies when dividing"
- Reply: Steve Kass: "Re: Precision and Scale innacuracies when dividing"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|