CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2008
    Posts
    16

    Question Precision -Pimpy needs help

    PHP Code:
    using System;

    public class 
    Pimpy
    {
       public static 
    void Main(String []a)
       {
           
    Double d=100.123456789123456789123456789123456789;
           
    Console.Writeline("{0}",d); 
       }

    I'd like the output to be decimally correct as defined, but only 16 digits after the dot is output.

    Someone please help me..

    Thanks

    -Pimpy-

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: Precision -Pimpy needs help


  3. #3
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: Precision -Pimpy needs help

    That is how Double type works: it has only 16 significant digits of precision. You need to use some arbitrary precision decimal class, like System.Decimal or something similar.
    B+!
    'There is no cat' - A. Einstein

    Use [code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  4. #4
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Precision -Pimpy needs help

    The best precision you can get using standard Types is using decimal values
    There you will get
    Code:
    decimal val = 100.123456789123456789123456789123456789M;
    Console.WriteLine(" {0}", val);
    // printout shows 100.12345678912345678912345679

    Rounded at the 26 th digit. But get it the total amount of digits is 29 here so if you have 1.123456789123456789123456789123456789M; then the answer will be 1.1234567891234567891234567891 All together always 29 digits. Simple try it.

    BTW Why do you need such an amount of precision ?
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  5. #5
    Join Date
    Sep 2008
    Posts
    16

    Re: Precision -Pimpy needs help

    Thank you for appeasing my anger. I have been trying it out all night only to fail again...and...again. I'd like to add in an M also at the tail of my Doubles but I feel like going without M because I have to read data from a file.
    I can only say Thanks but truly am unable to pay back anything if you could help me solve the problem.

    I need precision because I'd like to show the users the precision as many as they want, not just 29..

    Sincerely
    -Pimpy-

  6. #6
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Precision -Pimpy needs help

    Quote Originally Posted by Pimpya
    Thank you for appeasing my anger. I have been trying it out all night only to fail again...and...again. I'd like to add in an M also at the tail of my Doubles but I feel like going without M because I have to read data from a file.
    I can only say Thanks but truly am unable to pay back anything if you could help me solve the problem.

    I need precision because I'd like to show the users the precision as many as they want, not just 29..

    Sincerely
    -Pimpy-
    Hmm yea, maybe interesting in some math or physic applications but as you see this is standard.
    I have once long ago written code that was able to get a number with up to 63 digits as a string ( from a Textbox ) changed the value to binary also contained in an strings bit pattern and then written basic calculations for this like plus ( add), minus (substract), multiply and devide. But this was done in old VB 6.0 and was a lot of work. But Basically you can do this for your own. Transform the value digit by digit into bits (but you need to have a look on the real decimal value of it store them into a int64 ( or use a bit array ) and then figure out the mathematical operations for it, which are easy when doing it binary
    in the end the result you will have to retransfer into a string showing the result. But you have to really have a look what you are doing when multiplying two numbers both maybe 63 digits long. Where is the end in your precision. ? Nothing in the world is endless my dear. So if there is no real need forget this brilliant idea. In most normal cases 29 digits are more, much more then enough.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured