Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8908617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:13:36+00:00 2026-06-15T03:13:36+00:00

Below is the best representation I have been able to develop for calculating a

  • 0

Below is the best representation I have been able to develop for calculating a running sum inside a loop that’s nested inside a Parallel.for loop in VB.NET (Visual Studio 2010, .NET Framework 4). Note that when showing the results in `sum’ to the screen, there is a slight difference between the two sums, and hence loss of information in the parallelized variant. So how is the information being lost, and what’s happening? Can anyone offer some “microsurgery” on methodology for keeping a running sum in this context? (Note to new users of Parallel.for: I typically don’t use zero-based methods, so in the Parallel.for statement the I1 loops up to 101, since the code uses 101-1 as the upper bound. This is because MS developed the parallel code assuming zero-based counters):

    Dim sum As Double = 0
    Dim lock As New Object
    Dim clock As New Stopwatch
    Dim i, j As Integer
    clock.Start()
    sum = 0
    For i = 1 To 100
        For j = 1 To 100
            sum += Math.Log(0.9999)
        Next j
    Next i
    clock.Stop()
    MsgBox(sum & "  " & clock.ElapsedMilliseconds)
    sum = 0
    clock.Reset()
    clock.Start()
    Parallel.For(1, 101, Sub(i1)
                             Dim temp As Double = 0
                             For j1 As Integer = 1 To 100
                                 temp += Math.Log(0.9999)
                             Next
                             SyncLock lock
                                 sum += temp
                             End SyncLock
                         End Sub)
    clock.Stop()
    MsgBox(sum & "  " & clock.ElapsedMilliseconds)    
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:13:37+00:00Added an answer on June 15, 2026 at 3:13 am

    You are working with doubles and double are simply not accurate.
    In the non parallel loop, all errors are stored directly in sum. In the parallel loop you have an additional tmp that is later added to sum. Use the same tmp in your non parallel loop (adding to sum after the inner loop has run) and eventually the results wil be equal then.

     Dim sum As Double = 0
        Dim lock As New Object
        Dim clock As New Stopwatch
        Dim i, j As Integer
        clock.Start()
        sum = 0
        For i = 1 To 100
            For j = 1 To 100
                sum += Math.Log(0.9999)
            Next j
        Next i
        clock.Stop()
        Console.WriteLine(sum & "  " & clock.ElapsedMilliseconds)
        sum = 0
        clock.Reset()
    
        clock.Start()
        sum = 0
        For i = 1 To 100
            Dim tmp As Double = 0
            For j = 1 To 100
                tmp += Math.Log(0.9999)
            Next
            sum += tmp
        Next i
        clock.Stop()
        Console.WriteLine(sum & "  " & clock.ElapsedMilliseconds)
        sum = 0
        clock.Reset()
    
        clock.Start()
        Parallel.For(1, 101, Sub(i1)
                                 Dim temp As Double = 0
                                 For j1 As Integer = 1 To 100
                                     temp += Math.Log(0.9999)
                                 Next
                                 SyncLock lock
                                     sum += temp
                                 End SyncLock
                             End Sub)
        clock.Stop()
        Console.WriteLine(sum & "  " & clock.ElapsedMilliseconds)
    
    End Sub
    

    output:

    -1,00005000333357  0
    -1,00005000333347  0
    -1,00005000333347  26
    

    Conclusion: If you work with double, then (a + b) + c is NOT (always) equal to a + (b + c)

    UPDATE

    an even more simple example:

        Dim sum As Double
        For i = 1 To 100
            sum += 0.1
        Next
        Console.WriteLine(sum)
    
        sum = 0
        For i = 1 To 2
            Dim tmp As Double = 0
            For j = 1 To 50
                tmp += 0.1
            Next
            sum += tmp
        Next
        Console.WriteLine(sum)
    

    now the output is

    9,99999999999998
    10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can you please suggest the best approach for my requirement(below) in Java. I have
How to preprocess dictionary so that below operations are best supported a) search in
The code below explains best what I'm trying to accomplish. I know that I
Below is the best way I have found. Thoughts? If you load this up
The best solution I have right is like doing writing it like below. Before
Would following the table below be the best way of determining the access type
Is there a best (see below) way to append two byte arrays in C#?
Please suggest me the best authentication way to implement in the scenario mentioned below:
Of the below three functions: getc getchar & scanf which is the best one
Below is code to an inherited ComboBox. The issue is that the ComboBox is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.