PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 28th, 2009, 09:00 AM
kingg2 kingg2 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2009
Posts: 14 kingg2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 28 m 48 sec
Reputation Power: 0
Numeric vs Float

Hello I have a quick question. I am thinking of applying float to replace numeric. The numeric precision that I am using is (18,9). What float precision would be equivalent to numeric(18,9)? Is float faster than numeric?

Thanks

Reply With Quote
  #2  
Old April 28th, 2009, 09:18 AM
shammat shammat is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Oct 2003
Location: Germany
Posts: 2,701 shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 4 Days 2 h 27 m 57 sec
Reputation Power: 284
Quote:
Originally Posted by kingg2
Hello I have a quick question. I am thinking of applying float to replace numeric. The numeric precision that I am using is (18,9). What float precision would be equivalent to numeric(18,9)?
There is no equivalten as float stores an approximation of the value you put into it.

From the manual:
Quote:
Originally Posted by Manual
If you require exact storage and calculations (such as for monetary amounts), use the numeric type instead.
The question is, why would you want to replace the exact storage of a number with a data type that will not store the values the same way as you put them in?

Reply With Quote
  #3  
Old April 28th, 2009, 09:47 AM
kingg2 kingg2 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2009
Posts: 14 kingg2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 28 m 48 sec
Reputation Power: 0
This does not pertain to a monetary value. The element in the table is for a lightcurve that has around 7 decimal places.

Reply With Quote
  #4  
Old April 28th, 2009, 02:01 PM
shammat shammat is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Oct 2003
Location: Germany
Posts: 2,701 shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 4 Days 2 h 27 m 57 sec
Reputation Power: 284
Quote:
Originally Posted by kingg2
This does not pertain to a monetary value. The element in the table is for a lightcurve that has around 7 decimal places.
Monetary is just an example.

If you can live with the fact that the values you put into the database might not be the same as those that you get out, then you can surely use float.

But still: what is the reasoning for wanting to change it?

Reply With Quote
  #5  
Old April 28th, 2009, 02:06 PM
kingg2 kingg2 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2009
Posts: 14 kingg2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 28 m 48 sec
Reputation Power: 0
I want a faster data type than numeric. Any suggestions?

Reply With Quote
  #6  
Old April 28th, 2009, 02:37 PM
shammat shammat is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Oct 2003
Location: Germany
Posts: 2,701 shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 4 Days 2 h 27 m 57 sec
Reputation Power: 284
How do you know numeric is slow, and how would you test that a different one is faster?

Reply With Quote
  #7  
Old April 28th, 2009, 02:39 PM
kingg2 kingg2 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2009
Posts: 14 kingg2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 28 m 48 sec
Reputation Power: 0
I made a test table of 1,000 elements in one column. I then tested the select queries with the two different data types. After I would test one query I would copy the table and then drop the old table. I would then restart my postgres server to delete the cache. I did this 32 times for each data type.

Reply With Quote
  #8  
Old April 28th, 2009, 03:07 PM
shammat shammat is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Oct 2003
Location: Germany
Posts: 2,701 shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 4 Days 2 h 27 m 57 sec
Reputation Power: 284
And the result showed that float was faster?
Interesting, I would not have expected that.

How much faster is it?

Reply With Quote
  #9  
Old April 28th, 2009, 03:09 PM
kingg2 kingg2 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2009
Posts: 14 kingg2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 28 m 48 sec
Reputation Power: 0
No, actually numeric was faster... by around 2 milliseconds.

Reply With Quote
  #10  
Old April 28th, 2009, 03:59 PM
shammat shammat is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Oct 2003
Location: Germany
Posts: 2,701 shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 4 Days 2 h 27 m 57 sec
Reputation Power: 284
Quote:
Originally Posted by kingg2
No, actually numeric was faster... by around 2 milliseconds.
So there is no reason at all to use a datatype which doesn't store accurate numbers.
2 milliseconds is within the range of the accuracy of the measurement anyway, so basically the two are equally "fast".


The only thing I can think of that might be faster (at least for data retrieval), would be to store the numbers as integers and do the conversion in the front end.

You'll need to define a max. number of fractional digits though - which you also need to do with numeric but not with float.

Theb you'd store e.g. 0.0000001 as 1, 0.000001 as 10, 0.00001 as 100 and so on.

Reply With Quote
  #11  
Old April 28th, 2009, 06:15 PM
kingg2 kingg2 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2009
Posts: 14 kingg2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 28 m 48 sec
Reputation Power: 0
Thank you very much for your help. It was a pleasure talking with you!

Reply With Quote
  #12  
Old July 28th, 2009, 12:25 AM
wakerunner wakerunner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 68 wakerunner User rank is Private First Class (20 - 50 Reputation Level)wakerunner User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 12 h 49 m
Reputation Power: 5
This is something I discovered the hard way... after designing my database with double precision fields. I wish I would have searched more before settleing on the design. Why would anybody want to use a field that may or may not store the number you put into it? If you ever run a query on that field the results will be inconsistent, when there is a match it will not always be returned.




Quote:
Originally Posted by shammat
Monetary is just an example.

If you can live with the fact that the values you put into the database might not be the same as those that you get out, then you can surely use float.

But still: what is the reasoning for wanting to change it?

Reply With Quote
  #13  
Old January 11th, 2010, 09:47 AM
GRFrones GRFrones is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2010
Posts: 1 GRFrones User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 m 15 sec
Reputation Power: 0
There is an important conceptual difference between fixed point (numeric, integer, etc.) and floating point (float). Floating point is always an approximation, while fixed point is the exact value. However, the range of values you can store in a floating point field is much greater than fixed point.

For a number like 10^30, you would need a huge numeric field (and I don't think you can use numeric type in this case), but float should work.

In most business applications, there is no reason for using float, instead of numeric... however, in physics or statistics, for example, you can't use fixed point numbers.

Cheers.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > Numeric vs Float

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap