Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tFbsS-00EHT8-WC for pgsql-sql@arkaria.postgresql.org; Mon, 25 Nov 2024 16:23:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tFbsR-00GJwH-Cu for pgsql-sql@arkaria.postgresql.org; Mon, 25 Nov 2024 16:23:55 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tFbsQ-00GJw9-VM for pgsql-sql@lists.postgresql.org; Mon, 25 Nov 2024 16:23:55 +0000 Received: from mail-qk1-x735.google.com ([2607:f8b0:4864:20::735]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tFbsK-003iaz-2C for pgsql-sql@lists.postgresql.org; Mon, 25 Nov 2024 16:23:53 +0000 Received: by mail-qk1-x735.google.com with SMTP id af79cd13be357-7b1601e853eso296788885a.2 for ; Mon, 25 Nov 2024 08:23:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=americanefficient-com.20230601.gappssmtp.com; s=20230601; t=1732551827; x=1733156627; darn=lists.postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=wBb/CAQUgkKUbbNFR2q8wj0UyhNcPdTU6xwsv8sPiEw=; b=1Fi7DYhqubB4kY7WRMqlgwNnlBkA9lJ1Bo7oucJKpQM1zvB0tmflOVl6HFGx/6HtHB +gq6PWH2YOZksJbpBreLekHUaEC7kBf6492edYRNw9d4BHtUeF+5K/nqCbq9l8n+Rqxy vLWTas2uSZrtGz5VhhLImheSL9nEWs0GJnX1xyvhEP0MkkyhZ5Et3Obl84Svu9WACdyP faLHWG5LxRffW07iussYJR0Uff0FHdVr0KncUk0gH35Q9Re9LFqL0X9jjdJlbSr/oQM4 DmV0xQspB5M4r1o+jnVf/4WUXgVdki6Jt+DGlFwzr4a9v2Q2ewZk6FeM5bcEp81GeDxX iDkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732551827; x=1733156627; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wBb/CAQUgkKUbbNFR2q8wj0UyhNcPdTU6xwsv8sPiEw=; b=FnF06PR/ll5fnLhu4/xcHhdgEsaEDGEJ+uzGYD/vmmV5E3R23EI9Qcq3LVsPlupOvT GaavL2LnCOzhZ1udXru7o66QfwV+M6ngwT4gnU5AGfUa8pgY4TZEQq+1C9FyKzCTQKlR ve+ZSTCMpzEMibmojhT9BUPzInElXJ9biHfM0oC0Hi1GdeGE8FWrJnT2QKEYVRfyeScZ BNZs0N5HzRc9jNandoZRCFhl/kgNAI8TTaI4E9M8tIyvXnypwvkVH9lalMvBFg29QX4Z Ytbq7gCXQmsGF9aS4eqnQ5Ap8Hgqj/bH4neZXFVUCiTAouHxw58ZDz2rq+CzQI4s5ObP /m+Q== X-Forwarded-Encrypted: i=1; AJvYcCWRTFqK9Q7WPKe31eq8AmwXipDWwHvhUnUwmELoouM9x/I+qPWWaG5RqY1CId7fZvB/a684FXAffoE=@lists.postgresql.org X-Gm-Message-State: AOJu0YxNy+NXZH6IKMdfjuVM7pL3Rl6KkI1zuK2WWNxkDAaIUALwepnh eQ76c7Je/0XrZ5Qvur/2qo+im5f8CtZtCq9ugi6unhvqxYqT+iBwitijrgbwdfA= X-Gm-Gg: ASbGnctPximGRzd6rrgcXU+OCsHYigyy9BiyH38NHGFKQXtfxtY+EW0UFs1On8+BhzG g7IGopT/ggiLgNIGivXEbdUQ+EMTizaDRl4Ypchj0yQ601TojEaOQC8nobcB5NnzkM5/8M/ghv7 eg8ZlrImrTjfS4YDmdFTHUaAI3khV/6ifvZVA9IuxHGIL7bkqF9THeLs5ovpk3muFcYTvGOmCCn s6CA1LpAmWMNCPk7fBZ355CElUqjIfcePFm3Wo3ezUBuvDxQFHL79DfQVE45Wrfoho4sbKqhdQ= X-Google-Smtp-Source: AGHT+IH6xGBzyVcRKtwdnvUnJgLvRcse57u3qxao2hTbm59XnvhLYB9GGcSPSnpgc1VOjHdRLI42tw== X-Received: by 2002:a05:620a:3714:b0:7b1:534b:5385 with SMTP id af79cd13be357-7b51457e91fmr2182106385a.37.1732551827036; Mon, 25 Nov 2024 08:23:47 -0800 (PST) Received: from smtpclient.apple ([50.55.52.42]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7b66e0b0c55sm82884085a.87.2024.11.25.08.23.46 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Nov 2024 08:23:46 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.200.121\)) Subject: Re: Inconsistent results for division and multiplication operations From: Philip Semanchuk In-Reply-To: Date: Mon, 25 Nov 2024 11:23:36 -0500 Cc: szy <598546998@qq.com>, pgsql-sql Content-Transfer-Encoding: quoted-printable Message-Id: References: To: Erik Brandsberg X-Mailer: Apple Mail (2.3826.200.121) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On Nov 25, 2024, at 10:53=E2=80=AFAM, Erik Brandsberg = wrote: >=20 > This is a common issue with using floating point math. You will see = the same issue with many systems. Basically, the order of operations = can trigger very minor differences in results, but if you round the = first result to the same number of significant digits as the input, it = would be identical. = https://learn.microsoft.com/en-us/office/troubleshoot/access/floating-calc= ulations-info @szy Erik is right on target. The fine details of floating point math = confuse almost everyone at first encounter. If it makes you feel any = better, Python (and IIUC any other language that uses IEEE floating = point notation) exhibits the same quirk - $ python >>> 1.003/1.002*5.01 5.015 >>> 1.003*5.01/1.002 5.014999999999999 If you need extremely accurate representation of numbers (e.g. for = tracking money), use fixed precision (the numeric type in Postgres). = Math operations are much faster on floating point than on fixed = precision, so floating point is the default data type for non-integral = values. Fixed precision is more of a =E2=80=9Cuse as necessary=E2=80=9D = data type. Hope this helps Philip > On Mon, Nov 25, 2024 at 10:46=E2=80=AFAM szy <598546998@qq.com> wrote: >=20 > Hi PostgreSQL community, >=20 > I have observed inconsistent results when performing division and = multiplication operations in PostgreSQL. >=20 > postgres=3D# select 1.003/1.002*5.01; > ?column? > -------------------------- > 5.0149999999999999999806 =20 > (1 row) >=20 > postgres=3D# select 1.003*5.01/1.002; > ?column? > -------------------- > 5.0150000000000000 > (1 row) >=20 > However, the expected result should be consistent for both queries. = The actual results differ >=20 >=20 > szy > 598546998@qq.com > =20