Home All Groups Group Topic Archive Search About

Is this a bug: 2.2 - 0.4 = 1.8 but 1.2 - 0.4 = 0.8000001 ?

Author
15 Jul 2006 3:18 PM
Dave
Hi,

Dim a, b As Single
        a = 2.2
        b = 0.4
        Response.Write(a - b)

This gives: 1,8 (rendered according my regional settings)


But this gives: 0,8000001 (rendered according my regional settings):
Dim a, b As Single
        a = 1.2
        b = 0.4
        Response.Write(a - b)


Any explantion for this, and how to solve this?? ( i rounded on two digits)
Thanks
Dave

Author
15 Jul 2006 3:42 PM
Jay B. Harlow [MVP - Outlook]
Dave,
Try:

| Dim a, b As Decimal
|        a = 2.2D
|        b = 0.4D
|        Response.Write(a - b)

| But this gives: 0,8000001 (rendered according my regional settings):
Is given as that is the approximate value that a Single can hold. A Single
holds base 2 floating point numbers, some (most) base 10 floating point
numbers cannot be exactly represented

For details see:

http://www.yoda.arachsys.com/csharp/floatingpoint.html

http://www.yoda.arachsys.com/csharp/decimal.html


NOTE: The D in 2.2D indicates it is a Decimal literal as opposed to 2.2
which is a Double literal, as opposed to 2.2F which is a Single literal.


--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Show quoteHide quote
"Dave" <sd***@fgn.qc> wrote in message
news:OdejlHCqGHA.4684@TK2MSFTNGP05.phx.gbl...
| Hi,
|
| Dim a, b As Single
|        a = 2.2
|        b = 0.4
|        Response.Write(a - b)
|
| This gives: 1,8 (rendered according my regional settings)
|
|
| But this gives: 0,8000001 (rendered according my regional settings):
| Dim a, b As Single
|        a = 1.2
|        b = 0.4
|        Response.Write(a - b)
|
|
| Any explantion for this, and how to solve this?? ( i rounded on two
digits)
| Thanks
| Dave
|
|
Author
15 Jul 2006 4:06 PM
Dave
Thanks,

suppose i use variables a and b which are defined as decimals and gets their
values from  anywhere else.
How can i put the "D" after the variable :
c=a-b D




Show quoteHide quote
"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> schreef in
bericht news:ehgZCVCqGHA.2452@TK2MSFTNGP03.phx.gbl...
> Dave,
> Try:
>
> | Dim a, b As Decimal
> |        a = 2.2D
> |        b = 0.4D
> |        Response.Write(a - b)
>
> | But this gives: 0,8000001 (rendered according my regional settings):
> Is given as that is the approximate value that a Single can hold. A Single
> holds base 2 floating point numbers, some (most) base 10 floating point
> numbers cannot be exactly represented
>
> For details see:
>
> http://www.yoda.arachsys.com/csharp/floatingpoint.html
>
> http://www.yoda.arachsys.com/csharp/decimal.html
>
>
> NOTE: The D in 2.2D indicates it is a Decimal literal as opposed to 2.2
> which is a Double literal, as opposed to 2.2F which is a Single literal.
>
>
> --
> Hope this helps
> Jay B. Harlow [MVP - Outlook]
> .NET Application Architect, Enthusiast, & Evangelist
> T.S. Bradley - http://www.tsbradley.net
>
>
> "Dave" <sd***@fgn.qc> wrote in message
> news:OdejlHCqGHA.4684@TK2MSFTNGP05.phx.gbl...
> | Hi,
> |
> | Dim a, b As Single
> |        a = 2.2
> |        b = 0.4
> |        Response.Write(a - b)
> |
> | This gives: 1,8 (rendered according my regional settings)
> |
> |
> | But this gives: 0,8000001 (rendered according my regional settings):
> | Dim a, b As Single
> |        a = 1.2
> |        b = 0.4
> |        Response.Write(a - b)
> |
> |
> | Any explantion for this, and how to solve this?? ( i rounded on two
> digits)
> | Thanks
> | Dave
> |
> |
>
>
Author
15 Jul 2006 5:26 PM
Jay B. Harlow [MVP - Outlook]
| suppose i use variables a and b which are defined as decimals and gets
their
| values from  anywhere else.
If you define the variable as Decimal, the variable will hold a Decimal.

| c=a-b D

    a is a variable of type Decimal, it will return a Decimal value,
literals are not involved here, so you don't need the "literal type
character D".

The D is part of a Literal (constant if you will) value.

    2.2 is a literal

    a is a variable

    b is a variable

    c is a variable

    2.2 is a Double literal

    2.2D is a Decimal literal

    2.2F is a Single literal (the F stands for Float).


--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Show quoteHide quote
"Dave" <sd***@fgn.qc> wrote in message
news:uqcEViCqGHA.2452@TK2MSFTNGP03.phx.gbl...
| Thanks,
|
| suppose i use variables a and b which are defined as decimals and gets
their
| values from  anywhere else.
| How can i put the "D" after the variable :
| c=a-b D
|
|
|
|
| "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> schreef in
| bericht news:ehgZCVCqGHA.2452@TK2MSFTNGP03.phx.gbl...
| > Dave,
| > Try:
| >
| > | Dim a, b As Decimal
| > |        a = 2.2D
| > |        b = 0.4D
| > |        Response.Write(a - b)
| >
| > | But this gives: 0,8000001 (rendered according my regional settings):
| > Is given as that is the approximate value that a Single can hold. A
Single
| > holds base 2 floating point numbers, some (most) base 10 floating point
| > numbers cannot be exactly represented
| >
| > For details see:
| >
| > http://www.yoda.arachsys.com/csharp/floatingpoint.html
| >
| > http://www.yoda.arachsys.com/csharp/decimal.html
| >
| >
| > NOTE: The D in 2.2D indicates it is a Decimal literal as opposed to 2.2
| > which is a Double literal, as opposed to 2.2F which is a Single literal.
| >
| >
| > --
| > Hope this helps
| > Jay B. Harlow [MVP - Outlook]
| > .NET Application Architect, Enthusiast, & Evangelist
| > T.S. Bradley - http://www.tsbradley.net
| >
| >
| > "Dave" <sd***@fgn.qc> wrote in message
| > news:OdejlHCqGHA.4684@TK2MSFTNGP05.phx.gbl...
| > | Hi,
| > |
| > | Dim a, b As Single
| > |        a = 2.2
| > |        b = 0.4
| > |        Response.Write(a - b)
| > |
| > | This gives: 1,8 (rendered according my regional settings)
| > |
| > |
| > | But this gives: 0,8000001 (rendered according my regional settings):
| > | Dim a, b As Single
| > |        a = 1.2
| > |        b = 0.4
| > |        Response.Write(a - b)
| > |
| > |
| > | Any explantion for this, and how to solve this?? ( i rounded on two
| > digits)
| > | Thanks
| > | Dave
| > |
| > |
| >
| >
|
|
Author
15 Jul 2006 7:00 PM
Dave
Thanks again

Show quoteHide quote
"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> schreef in
bericht news:%23noZQPDqGHA.3288@TK2MSFTNGP03.phx.gbl...
>| suppose i use variables a and b which are defined as decimals and gets
> their
> | values from  anywhere else.
> If you define the variable as Decimal, the variable will hold a Decimal.
>
> | c=a-b D
>
>    a is a variable of type Decimal, it will return a Decimal value,
> literals are not involved here, so you don't need the "literal type
> character D".
>
> The D is part of a Literal (constant if you will) value.
>
>    2.2 is a literal
>
>    a is a variable
>
>    b is a variable
>
>    c is a variable
>
>    2.2 is a Double literal
>
>    2.2D is a Decimal literal
>
>    2.2F is a Single literal (the F stands for Float).
>
>
> --
> Hope this helps
> Jay B. Harlow [MVP - Outlook]
> .NET Application Architect, Enthusiast, & Evangelist
> T.S. Bradley - http://www.tsbradley.net
>
>
> "Dave" <sd***@fgn.qc> wrote in message
> news:uqcEViCqGHA.2452@TK2MSFTNGP03.phx.gbl...
> | Thanks,
> |
> | suppose i use variables a and b which are defined as decimals and gets
> their
> | values from  anywhere else.
> | How can i put the "D" after the variable :
> | c=a-b D
> |
> |
> |
> |
> | "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> schreef
> in
> | bericht news:ehgZCVCqGHA.2452@TK2MSFTNGP03.phx.gbl...
> | > Dave,
> | > Try:
> | >
> | > | Dim a, b As Decimal
> | > |        a = 2.2D
> | > |        b = 0.4D
> | > |        Response.Write(a - b)
> | >
> | > | But this gives: 0,8000001 (rendered according my regional settings):
> | > Is given as that is the approximate value that a Single can hold. A
> Single
> | > holds base 2 floating point numbers, some (most) base 10 floating
> point
> | > numbers cannot be exactly represented
> | >
> | > For details see:
> | >
> | > http://www.yoda.arachsys.com/csharp/floatingpoint.html
> | >
> | > http://www.yoda.arachsys.com/csharp/decimal.html
> | >
> | >
> | > NOTE: The D in 2.2D indicates it is a Decimal literal as opposed to
> 2.2
> | > which is a Double literal, as opposed to 2.2F which is a Single
> literal.
> | >
> | >
> | > --
> | > Hope this helps
> | > Jay B. Harlow [MVP - Outlook]
> | > .NET Application Architect, Enthusiast, & Evangelist
> | > T.S. Bradley - http://www.tsbradley.net
> | >
> | >
> | > "Dave" <sd***@fgn.qc> wrote in message
> | > news:OdejlHCqGHA.4684@TK2MSFTNGP05.phx.gbl...
> | > | Hi,
> | > |
> | > | Dim a, b As Single
> | > |        a = 2.2
> | > |        b = 0.4
> | > |        Response.Write(a - b)
> | > |
> | > | This gives: 1,8 (rendered according my regional settings)
> | > |
> | > |
> | > | But this gives: 0,8000001 (rendered according my regional settings):
> | > | Dim a, b As Single
> | > |        a = 1.2
> | > |        b = 0.4
> | > |        Response.Write(a - b)
> | > |
> | > |
> | > | Any explantion for this, and how to solve this?? ( i rounded on two
> | > digits)
> | > | Thanks
> | > | Dave
> | > |
> | > |
> | >
> | >
> |
> |
>
>