Click here to Skip to main content
16,016,088 members
Home / Discussions / C#
   

C#

 
GeneralRe: float calculation issue Pin
George_George3-Jun-08 21:57
George_George3-Jun-08 21:57 
GeneralRe: float calculation issue Pin
The Nightcoder4-Jun-08 2:55
The Nightcoder4-Jun-08 2:55 
GeneralRe: float calculation issue Pin
George_George4-Jun-08 22:31
George_George4-Jun-08 22:31 
GeneralRe: float calculation issue Pin
The Nightcoder4-Jun-08 23:08
The Nightcoder4-Jun-08 23:08 
GeneralRe: float calculation issue Pin
George_George4-Jun-08 23:28
George_George4-Jun-08 23:28 
AnswerRe: float calculation issue (decimal is no solution) [modified] Pin
The Nightcoder28-May-08 23:12
The Nightcoder28-May-08 23:12 
GeneralRe: float calculation issue (decimal is no solution) Pin
George_George31-May-08 3:28
George_George31-May-08 3:28 
GeneralRe: float calculation issue (decimal is no solution) Pin
The Nightcoder2-Jun-08 3:02
The Nightcoder2-Jun-08 3:02 
George_George wrote:
1. Why using Decimal type, we can represent 0.1 precisely? Because Decimal type will use base 10 other than base 2 to represent a number?


Exactly. Internally, I beleive that the C# decimal is represented as a scaled binary integer, but it behaves as if it were a "true" decimal type. And as the names imply, "decimal"=base 10, "binary"=base 2.

George_George wrote:
2. For the numbers (which can be precisely represented by base 2), are there any possibilities which they can not be represented by base 10 precisely? If yes, I think it is not correct to say Decimal has less issues compared with float type?


Regardless of base (10, 2 or whatever), some real numbers won't have a finite decimal (binal is the correct word in Swedish using base 2, I don't know the English word) representation. In base 2 I gave 1/10 (0.1) as an example, in base 2 I gave 1/3 as one. But there are others - infinitely many, in fact. This means that decimal floating-point types don't have less issues than binary floating-point types. They just have other issues.

One exception: Binary floating point types can confuse people who don't understand a) that they are binary or b) what that means. Decimal types don't have that issue. In all other respects (precision limits, error accumulation and whatever) they are identical.

Like I mentioned previously, though, there are also decimal fixed-point types (as in those you create using decimal(13,4) in SQL. These are even easier to understand for people working with economics, as the round everything to the specified number of decimals all the time. They're usually quite useless for advanced calculations, though.

George_George wrote:
3. Both float and double will use base 2 as internal representation?


Yes, they differ only in precision, range and size - see System.Single (=C# float) and System.Double (=C# double) in the documentation for details. A look at System.Decimal (=C# decimal) could be informative, too...

Peter the small turnip

(1) It Has To Work. --RFC 1925[^]

GeneralRe: float calculation issue (decimal is no solution) Pin
George_George3-Jun-08 2:09
George_George3-Jun-08 2:09 
GeneralRe: float calculation issue (decimal is no solution) Pin
The Nightcoder3-Jun-08 2:48
The Nightcoder3-Jun-08 2:48 
GeneralRe: float calculation issue (decimal is no solution) Pin
George_George3-Jun-08 3:00
George_George3-Jun-08 3:00 
GeneralRe: float calculation issue (decimal is no solution) Pin
The Nightcoder3-Jun-08 3:37
The Nightcoder3-Jun-08 3:37 
GeneralRe: float calculation issue (decimal is no solution) Pin
George_George3-Jun-08 21:53
George_George3-Jun-08 21:53 
GeneralRe: float calculation issue (decimal is no solution) Pin
The Nightcoder4-Jun-08 3:04
The Nightcoder4-Jun-08 3:04 
GeneralRe: float calculation issue (decimal is no solution) Pin
George_George4-Jun-08 22:22
George_George4-Jun-08 22:22 
GeneralRe: float calculation issue (decimal is no solution) Pin
The Nightcoder4-Jun-08 23:01
The Nightcoder4-Jun-08 23:01 
GeneralRe: float calculation issue (decimal is no solution) Pin
George_George4-Jun-08 23:27
George_George4-Jun-08 23:27 
GeneralRe: float calculation issue (decimal is no solution) Pin
The Nightcoder5-Jun-08 4:01
The Nightcoder5-Jun-08 4:01 
GeneralRe: float calculation issue (decimal is no solution) Pin
George_George5-Jun-08 14:48
George_George5-Jun-08 14:48 
QuestionThreadpool Synchronization causing out of memory error Pin
sumathi pandarinathan26-May-08 0:12
sumathi pandarinathan26-May-08 0:12 
AnswerRe: Threadpool Synchronization causing out of memory error Pin
leppie26-May-08 4:03
leppie26-May-08 4:03 
GeneralRe: Threadpool Synchronization causing out of memory error Pin
sumathi pandarinathan26-May-08 21:09
sumathi pandarinathan26-May-08 21:09 
Questionfolderbrowserdialog Pin
huotari25-May-08 23:57
huotari25-May-08 23:57 
AnswerRe: folderbrowserdialog Pin
Dario Solera26-May-08 0:18
Dario Solera26-May-08 0:18 
AnswerRe: folderbrowserdialog Pin
D i x y26-May-08 0:56
D i x y26-May-08 0:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.