locked
VB changin value of hardcoded double constants RRS feed

  • Question

  • Hello, after working with some VB.net files in a project, I find that somehow the values of hard coded numeric double values have changed,  that is:

    6.4 has been changed to 6.4000000000000004

    0.07 has been changed to 0.070000000000000007

    1.4 has been changed to 1.3999999999999999

    This has happened in a lot of the source files of the project. Is this normal behavior? When does this replacement take place? While I can understand that the original numbers cannot be exactly represented as double, I do not want this substitution, is there some way to prevent them?

    Initially I thought that was some behavior of the source control system (TFS) but I really dot know...

    VS 2010 + TFS 2010


    oscar
    Tuesday, February 15, 2011 1:12 PM

All replies

  • As you say, the original numbers cannot be exactly represented as double, so they have to be changed. If you want to avoid the change, you can use the Decimal type instead of Double.
    Tuesday, February 15, 2011 2:07 PM
  • OK, but when does this change take place? I worked with these files for a while and then suddenly found them changed
    oscar
    Tuesday, February 15, 2011 2:29 PM
  • Hi oscarg,

    Are you actually using Const in your declarations for each Constant ?

     

    Public Class Form1
    
      Const doubleNumber1 As Double = 6.4
      Const doubleNumber2 As Double = 0.07
      Const doubleNumber3 As Double = 1.4
    
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        MessageBox.Show(doubleNumber1.ToString)
        MessageBox.Show(doubleNumber2.ToString)
        MessageBox.Show(doubleNumber3.ToString)
    
      End Sub
    End Class

     

    Maybe you are using a different TYPE in a database and somehow the

    values are getting changed when you read them back in from the database?

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    Tuesday, February 15, 2011 3:19 PM
  • Hi, this happens in SOURCE CODE, the database has nothing to do with it.

    That is, the line

    l_caja = coh("montoa") * 0.006
    

    where l_caja and the data column are both double

     

     

    gets modified in the source file to

    l_caja = coh("montoa") * 0.0060000000000000001
    

     

     

    The constants are written inline as in the example above

    More strange yet, sometimes "edit->advanced-> format document" reverts to the original text, Could this be some hidden VB option?

     


    oscar
    Tuesday, February 15, 2011 4:18 PM
  • I see what you mean. I wasn't aware of the source code being changed like that. However, I believe that 0.006 would be treated as 0.006000000000000001 even if the source code wasn't changed.

    You should still be able to force it to be treated as a Decimal literal instead of Double by using the "D" literal type character:

    l_caja = coh("montoa") * 0.006D
    

    Tuesday, February 15, 2011 4:50 PM
  • I agree that this might be the actual representation of 0.006 as double, however I cannot find how to control these behavior (changing of the code in source files) and when it does happen.

     


    oscar
    Tuesday, February 15, 2011 5:24 PM
  • Hello oscarg,

    Thanks for your post.

    I am sorry I could not understand this issue very clearly. Do you mean when you calculate the value of l_caja using

    l_caja = coh("montoa") * 0.006
    

    Then the code will be changed  in the source file? You said the database has nothing to do with it, if you only define a double variable and set its value as 0.006 as John suggested, will its value be changed in the source file? Could you please provide a simple example?

    l_caja = coh("montoa") * 0.0060000000000000001
    
     
    If I misunderstood, please feel free to follow up.

    Have a nice day.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

     

    Friday, February 18, 2011 4:27 PM
  • I guess it changes as soon as it goes into the Double. Double allows you to store values from 1.7E–308 to 1.7E+308 using 64 bits to a minimum of 15 points of precision.

    It can represent 6.4000000000000004, it can't represent 6.4000000000000000

    (A bit like how you can't represent 1.5 in an Integer.)

    It's the same in all languages. .Net uses international standards for it's floating point types.

    It's a problem that arises at the fundamental level - maths. You can only represent a limited number of values with a given number of bits.

    http://www.codeproject.com/KB/dotnet/ExtremeFloatingPoint1.aspx

    Saturday, February 19, 2011 8:33 AM
  • Hello oscarg,

    What about the problem on your side now? If you have any concerns, please feel free to follow up.

    Have a nice day.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 21, 2011 3:08 AM
  • Sorry about the delay

    YES, it changes value in the source file, but I cannot find when. After some time without working with the source, I checked it out and saw the changes. Formatted the document and it reverted to the original values. I makes me uneasy not to know when the changes are made


    oscar
    Monday, February 21, 2011 7:19 PM
  • I suppose that you are uneasy...thinking perhaps you can circumvent that...you cannot, if you use Single, or Double...they use IEEE-754.

    The hexadecimal values for your constant are:

    40 19 99 99 99 99 99 9A

    That is not what is changing....in fact, nothing is changing, except the TEXTUAL representation that you are seeing is now 6.4000000000000004

    However, the underlying value remains 40 19 99 99 99 99 99 9A

    Your post implies that the value has been changed in the source file....where is this occurring? Constants do not work that way, but MACROS do....are you using a MACRO?

    AAR, the value will assume the value of the double closest to what you typed in the definition...that happens at compile time....so, it is all over your code, wherever you use that constant.

    In the past, people converted decimals to integers before doing the math, and back to decimals afterwards, which really only gets rid of the issue...in the middle. In practice, it is of little consequence, until you 'forget' to factor it in....

    Monday, February 21, 2011 9:10 PM
  • I know what changes is the textual representation. However I do not like to write "myvar = 6.4" in my source file, and doing a TFS compare some time later on my source  discover that between version xxxxx0 and xxxxx1 the source text changed from 6.4 to  6.4000000000000004... what else does VS change without any warning or question?
    oscar
    Monday, February 21, 2011 10:18 PM
  • jo0ls,

    the point the OP is trying to make is that his *source file* is being changed!  He has typed in the value 6.4 and sometime later when he looks again at the *source file*, it has been changed!  This is not occurring at run time, but in the IDE code editor.

    I can only guess that it is happening during compile time.

    Is this in auto-generated code?

    Tuesday, February 22, 2011 6:39 PM
  • I still don't know where this change is happening...you haven't shown an example, only related your symptom.

    I am stubborn, so...I found out one way to get VisualStudio to change it, HOWEVER....in my case, it only changes it when you press the ENTER key when entering it...if you DO NOT PRESS ENTER, it does not change....at least, I cannot make it change.

     

    Const myPI As Double = 3.1428571428571428571428571428571

    Try it with that.....if you press ENTER, it truncates it. Otherwise, it remains as typed in. IT ALSO DOES IT WHEN PASTING...I think you have to type it in without using enter!

     

     

    Const myPI As Double = 3.1428571428571428571428571428571

     

    Const _myPI As Double = 3.1428571428571428

     

    Tuesday, February 22, 2011 7:53 PM
  • So you want some code...

         l_registro("l_dbase") = l_diasbase
         l_registro("base_a") = Base_A
         l_registro("diasp_a") = l_diasp_a * 1.3999999999999999
         l_registro("diasp") = l_diasp * 1.3999999999999999
         l_registro("DiasProg") = Diasprog
         l_registro("sit_lab") = dr("sit_lab")
    
    I did never write 1.3999999. I wrote 1.4 in both lines. Some time later, I was working with the file again and in in the IDE I see the code above. However, I check out, and go to edit->advanced-> format document and it reverts to
         l_registro("base_a") = Base_A
         l_registro("diasp_a") = l_diasp_a * 1.4
         l_registro("diasp") = l_diasp * 1.4
         l_registro("DiasProg") = Diasprog
    
    When does the ide change this stuff, I would like to know.
    oscar
    Wednesday, February 23, 2011 6:38 PM
  • Hello oscarg,

    As far as I know, TFS could not change the source file. What do you mean "doing a TFS compare some time later on my source  discover that between version xxxxx0 and xxxxx1 the source text changed from 6.4 to  6.4000000000000004." ? Do you use any third party products? Have you tried on other computers? Do you get the same scenario?

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 24, 2011 10:03 AM
  • Well, I mean exactly that, there were some branches and merges between, but no third party tools involved.
    oscar
    Thursday, February 24, 2011 5:00 PM
  • I had the same issue with VB.net.

    I had to write

    x = 1.134

    and as long as i finished to write this line, the IDE changed it to:

    x = 1.1339999999999999

    It was really unpleasant. I changed the code using Decimal instead of Double and used:

    x = 1.134D

    And everything was ok.

    Best regards.

    Tuesday, May 15, 2012 5:04 PM