Skip to main content

Float vs. Decimal data types in Sql Server

 

This is an excellent article describing when to use float and decimal. Float stores an approximate value and decimal stores an exact value.

In summary, exact values like money should use decimal, and approximate values like scientific measurements should use float.

Here is an interesting example that shows that both float and decimal are capable of losing precision. When adding a number that is not an integer and then subtracting that same number  float results in losing precision while decimal does not:

DECLARE @Float1 float, @Float2 float, @Float3 float, @Float4 float;
SET @Float1 = 54;
SET @Float2 = 3.1;
SET @Float3 = 0 + @Float1 + @Float2;
SELECT @Float3 - @Float1 - @Float2 AS "Should be 0";

Should be 0
----------------------
1.13797860024079E-15

 

When multiplying a non integer and dividing by that same number, decimals lose precision while floats do not.

DECLARE @Fixed1 decimal(8,4), @Fixed2 decimal(8,4), @Fixed3 decimal(8,4);
SET @Fixed1 = 54;
SET @Fixed2 = 0.03;
SET @Fixed3 = 1 * @Fixed1 / @Fixed2;
SELECT @Fixed3 / @Fixed1 * @Fixed2 AS "Should be 1";

Should be 1
---------------------------------------
0.99999999999999900

Comments

Unknown said…
You've got some fuzzy math going on here. Decimals do not lose precision.

If 1800 = 1 * (54 / .03)
Then 1 = 1800 / (54 / .03)

Your formula shows:
1 = 1800 / 54 * .03

Huge flaw in your arithmetic.
Unknown said…
Nice post very helpful

dbakings
Daisy Thomas said…
An impressive share! I have Malaysia free game live22 just forwarded this onto a coworker who has been doing a little research on this. And he in fact bought me dinner because I discovered it for him... lol. So let me reword this.... Thank YOU for the meal!! But yeah, thanks for spending time to talk about this topic here on your website. The Gaming Club bears a license from the government of Gibraltar, and claims to be one of a prefer few casinos that have a license from the Gibraltar government. A devotee of the Interactive Gaming Council (IGC), The Gaming Club follows all the guidelines laid beside by the organization, something that has in imitation of a long quirk in it swine attributed as a good place to gamble online.

Everything about The Gaming Club feels good; be it the promotions, the big number of games, the multipart banking options upon offer, the unprejudiced security measures, or the fair and held responsible gaming practices the casino adopts.

The Gaming Club motors along on software developed by one of the giants of online gaming software press forward Microgaming. The software it uses is broadminded and has a range of features designed to complement your online gambling experience and make you desire to arrive urge on after every circular of gambling you do here.

Another hallmark of a good casino is the quality of its customer retain team, and The Gaming Club does not disappoint upon this front.
http://magnum4dlive.com/

Popular posts from this blog

How to Create and Run Tableau Bridge on Linux Containers

Tableau Bridge is now availble on Linux Containers. Yay! Now what does this mean and how do I build and run Linux Containers? We will discuss the advantages of running Bridge on Linux Containers the steps to build them, and finally, we will provide some automation script ideas for monitoring and scaling Linux Bridge agents. Tableau Bridge Today Until recently, Tableau Bridge was only available as a Windows application running on a Windows VM. It supported only one bridge agent per Virtual or Physical Machine. Advantages of Bridge in Containers Better Hardware Utilization: Linux containers are more efficient than Windows VMs, requiring only about 1/50th of the disk space. Ability to Spin Up Multiple Bridge Agents: With Linux Containers, it becomes easier to spin up multiple bridge agents on a single machine, improving scalability and resource utilization. Infrastructure Automation: Linux Containers enable easier automation of provisioning bridge agents and upgrading Tableau Bridge, the...

RAM Disks do not speed up Visual Studio

  The limiting factor for Visual Studio is disk IO. I got a tip to speed up Visual Studio from Channel 9 by creating a RAM disk which sounded like a great idea. However, when I ran a thorough set of tests, I found that the performance difference between the Ram disk and the hard disk were not appreciably different. This was a big surprise since RAM is 240,000 times faster than disk (see my previous blog post). But the reason is because Visual Studio and Vista do a lot of caching. So compile times for the same project in RAM disk and on hard disk were pretty similar. I also tested the time it took to search the entire solution for a word, and times to open a solution. There was no discernable difference!   If you still want to try it out and create your own RAM disk, you can download a simple RAMDISK.EXE utility to create a RAM disk in just a few minutes. What is a RAM Disk ?   Ramdisk is a virtual drive created in RAM.   Performance Analysis Creating f...

SpreadsheetGear vs. SyncFusion vs. ComponentOne

I conducted a three month analysis comparing three top spreadsheet controls for .NET application developers: SpreadsheetGear , SyncFusion Spreadsheet , and ComponentOne Spread.NET . The definite winner was SpreadsheetGear, with ComponentOne Spread.net in second. SpreadsheetGear provides the most Excel-like experience, is the most performant, and provides the most responsive and capable product support. I build a reporting engine for my company and SpreadsheetGear made it awesome. Our customers have been very pleased. I wanted to share my extensive analysis with any other developers out there trying to find the right spreadsheet control for their .NET application.   Comparison Spreadsheet gear grid goes to 1 million! Wow. (SyncFusion goes to 500 by default, ComponentOne goes to 60k) SpreadsheetGear is an older control which has been around for a while. It is built by the same guys who built Formula1, so they have been doing spreadsheets for many years. Sp...