Skip to content
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

double precision field not parsed correctly, loss of precision #64

Open
gabooh opened this issue Jul 4, 2017 · 4 comments
Open

double precision field not parsed correctly, loss of precision #64

gabooh opened this issue Jul 4, 2017 · 4 comments

Comments

@gabooh
Copy link

gabooh commented Jul 4, 2017

I get data from fields in postgresql 9.4 :

create table bookseller
(
...
	maplatitude double precision,
	maplongitude double precision,
...
);

And setting up node-pg like this :


const {Pool} = require('pg')
const types = require('pg').types

types.setTypeParser(701, (val) => {
  console.log(`val double : ${val}`)
  return parseFloat(val)
})

Data from pg looks like this :

maplatitude : 43.84668200000001
maplongitude : 5.559941999999999

But I get this with node-pg, whatever I do (default type parser, or with above override).

maplatitude : 43.846682
maplongitude : 5.559942

The result from node-pg :

Result {
  command: 'SELECT',
  rowCount: 1,
  oid: NaN,
  rows: 
   [ anonymous {
     ...
       maplatitude: 43.846682,
       maplongitude: 5.559942 } ],
  fields: 
...
     Field {
       name: 'maplatitude',
       tableID: 724451,
       columnID: 11,
       dataTypeID: 701,
       dataTypeSize: 8,
       dataTypeModifier: -1,
       format: 'text' },
     Field {
       name: 'maplongitude',
       tableID: 724451,
       columnID: 12,
       dataTypeID: 701,
       dataTypeSize: 8,
       dataTypeModifier: -1,
       format: 'text' } ],

I've read countless bug report and node-pg docs on this, seams like it should work correctly. Is this a bug ? Or something I'm not doing correctly ?

@gabooh
Copy link
Author

gabooh commented Jul 5, 2017

In case that helps, using pg-native returns the same truncated data :

var Client = require('pg-native')

var client = new Client()
client.connectSync('postgresql://xxxx', (err) => {
  throw err
})

var rows = client.querySync(
  `
    SELECT id, maplatitude, maplongitude 
    FROM xxx 
    WHERE id = xxx
    LIMIT 10
  `)
console.log(rows)

@gabooh
Copy link
Author

gabooh commented Jul 5, 2017

And I have a workaround : it does not work correctly with double precision type, but it's ok with Numeric type and using setTypeParser.
I'll leave this open, as I think the behaviour should be the same with double precision type. There is data loss.

@charmander
Copy link
Contributor

You’ll need to set extra_float_digits (to 3, for example) – see the note at https://www.postgresql.org/docs/10/static/datatype-numeric.html#DATATYPE-FLOAT.

@xajhqffl
Copy link

I got similar issues.
it is losing precision with node-pg. I used same pg client in Java, it is returning correct value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants