question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JSON value truncate floating numbers

See original GitHub issue

Bug description

When I store a JSON value with floating numbers, Prisma seems to truncate the number and this leads to loss of precision and incorrect values.

How to reproduce

await prisma.user.create({
  data: {
    color: {
      r: 0.92156863212585449,
      g: 0.34117648005485535,
      b: 0.34117648005485535,
    },
  }
})

The logged query is (which makes me think this comes from Prisma and not from the database):

{
  timestamp: 2020-10-14T11:52:11.040Z,
  query: 'INSERT INTO "public"."User" ("id","color") VALUES ($1,$2) RETURNING "public"."User"."id"',
  params: '["ckg9c3vha0035qns5jyeyy8pb",{"r":0.9215686321258544,"g":0.34117648005485535,"b":0.34117648005485535}]',
  duration: 0,
  target: 'quaint::connector::metrics'
}

Notice the ending of the r number, it’s truncated. But not for the b or g value.

Expected behavior

The stored value for r should be 0.92156863212585449 and not 0.9215686321258544.

Prisma information

model User {
  color Json
}

Environment & setup

  • OS: MacOS
  • Database: PostgreSQL
  • Node.js version: v12.16.2
  • Prisma version:
@prisma/cli          : 2.8.1
Current platform     : darwin
Query Engine         : query-engine 439da16b2f8314c6faca7d2dad2cdcf0732e8a9c (at node_modules/@prisma/cli/query-engine-darwin)
Migration Engine     : migration-engine-cli 439da16b2f8314c6faca7d2dad2cdcf0732e8a9c (at node_modules/@prisma/cli/migration-engine-darwin)
Introspection Engine : introspection-core 439da16b2f8314c6faca7d2dad2cdcf0732e8a9c (at node_modules/@prisma/cli/introspection-engine-darwin)
Format Binary        : prisma-fmt 439da16b2f8314c6faca7d2dad2cdcf0732e8a9c (at node_modules/@prisma/cli/prisma-fmt-darwin)
Studio               : 0.296.0

Related

This could be related https://github.com/prisma/prisma/issues/3479 https://github.com/prisma/prisma/issues/2903

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
timsuchanekcommented, Oct 29, 2020

@Kerumen thanks for the clarification. That then indeed is a bug. The Decimal type will not fix this one, as you have to use it outside of Json. We’ll further investigate 🙏

0reactions
pimeyscommented, Nov 4, 2020

There’s a fix coming. The problem in general is how JSON doesn’t use actual f64’s to represent the numbers, so converting them to standard floats may cause accuracy errors and this is what we see here.

Luckily Rust’s JSON library has a compile time flag, that uses slower but one to one accurate conversion to and from JSON. This is enough with PostgreSQL.

Then, again, if anybody ever reads this issue, do not try to store floats in MySQL’s JSON data type:

create table foo (id int primary key, data json);
insert into foo (id, data) values (1,'0.9215686321258545');
select data from foo;

Returning 0.9215686321258544

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to truncate float to %.2f format while encoding it to JSON?
It seems, that I can use the constrctor JSONObject(String json), something like this:
Read more >
Numeric functions · JSONata
Returns the value of number rounded down to the nearest integer that is smaller or equal to number . If number is not...
Read more >
Floating Point in JSON Without Loss of Precision
Blog explains how to express floating point numbers in their binary form, or an equivalent form, when encoding into JSON, without losing ...
Read more >
How to configure the serialization of floats? | ArduinoJson 6
When you call serializeJson() , ArduinoJson converts each floating point value into a string with a variable number of decimal places.
Read more >
IT32239: FLOAT VALUE MIGHT BE TRUNCATED IN API ... - IBM
After using context.set to set a context variable to a JSON object which contains a float number, the JSON value of the float...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found