نکات کاربردی

طبقه بندی موضوعی

آخرین مطالب

۲ مطلب با کلمه‌ی کلیدی «float» ثبت شده است

It is not a good approach to use double for precise values, such as currency.

BigDecimal for the Rescue

BigDecimal represents a signed decimal number of arbitrary precision with an associated scale. BigDecimal provides full control over the precision and rounding of the number value. Virtually, it's possible to calculate the value of pi to 2 billion decimal places using BigDecimal, with available physical memory being the only limit.

That’s the reason why we should always prefer BigDecimal or BigInteger for financial calculations.

 

Special Notes

Primitive type: int and long are also useful for monetary calculations if the decimal precision is not required.

We should really avoid using the BigDecimal (double value) constructor, and instead, we should really prefer using the BigDecimal(String), because BigDecimal (0.1) results in (0.1000000000000000055511151231257827021181583404541015625) being stored in the BigDecimal instance. In contrast, BigDecimal ("0.1") stores exactly 0.1.


What Every Computer Scientist Should Know About Floating-Point Arithmetic

behrad nasehi
۱۴ تیر ۰۰ ، ۱۴:۱۰ موافقین ۰ مخالفین ۰ ۰ نظر

The float and real data types are known as approximate data types. The behavior of float and real follows the IEEE 754 specification on approximate numeric data types.

Approximate numeric data types do not store the exact values specified for many numbers; they store an extremely close approximation of the value. For many applications, the tiny difference between the specified value and the stored approximation is not noticeable. At times, though, the difference becomes noticeable. Because of the approximate nature of the float and real data types, do not use these data types when exact numeric behavior is required, such as in financial applications, in operations involving rounding, or in equality checks. Instead, use the integer, decimal, money, or smallmoney data types.

Avoid using float or real columns in WHERE clause search conditions, especially the = and <> operators. It is best to limit float and real columns to > or < comparisons.

The IEEE 754 specification provides four rounding modes: round to nearest, round up, round down, and round to zero. Microsoft SQL Server uses round up. All are accurate to the guaranteed precision but can result in slightly different floating-point values. Because the binary representation of a floating-point number may use one of many legal rounding schemes, it is impossible to reliably quantify a floating-point value
https://technet.microsoft.com/en-us/library/ms187912(v=sql.105).aspx

گروه تخصصی Sql Server


کلا در sql نوع های عددی رو به دو دسته تقسیم می کنیم دسته اول که بهشون approximate data types میگیم مانند float و real
به این معنا که عددی که شما قصد ذخیره اون در فیلدی با ابن نوع رو دارین، به صورت دقیق ذخیره نمیشه و یه تخمین بسیار نزدیک به اون ذخیره می شود
و اما سایر نوع های داده ای عددی integer, decimal, money, وsmallmoney و ... بر خلاف نوع داده ای float - real دقیقا مقدار عددی وارد شده رو ذخیره می کنند.

کاربرد Decimal - Numeric , ... :
1-زمانی که شما نیاز به انجام محاسبات دقیق دارید. برنامه های حسابداری - مالی - انبار و ...
2-استفاده از این ستون در شرط Where و عملگرهای = and <>

کاربرد نوع داده ای Float - Real:
دقت کنید زمانی که نیاز به نگهداری مقادیر اعشاری در یک جدول باشد دو مفهوم مطرح می شود:
Precision: تعداد کل ارقامی که یک نوع داده ای نگهداری می کند
Scale: تعداد ارقامی که در سمت راست (ارقام صحیح) قرار میگیرند

دو نوع داده ای numeric , decimal مقدار Precision برابر با 38 می باشد. بنابراین اگر نیاز به نگهداری مقادیری در جدولتون دارین که تعداد ارقام اون از 38 بیشتر باشد این نوع های داده ای مناسب هستند.

نتیجه گیری:
از نوع داده ای decimal و numeric زمانی که دقیق بودن اعداد و محاسبات برای شما مهم و حیاتی هستند استفاده کنید
از نوع های داده ای Flat ,Real در مقایسه ها (= و < و >) در شرط where استفاده نکنید
behrad nasehi
۲۳ فروردين ۹۷ ، ۱۳:۴۷ موافقین ۰ مخالفین ۰ ۰ نظر