dbasnett's Profile User Rating:

Reputation: 113 Stalwart
Group:
Contributors
Active Posts:
618 (0.25 per day)
Joined:
01-October 08
Profile Views:
8,616
Last Active:
Today, 05:34 AM
Currently:
Offline

Previous Fields

Country:
Who Cares
OS Preference:
Who Cares
Favorite Browser:
Who Cares
Favorite Processor:
Who Cares
Favorite Gaming Platform:
Who Cares
Who Cares
Dream Kudos:
50

Latest Visitors

dbasnett has not set their status

1. In Topic: Generate Prime Numbers within inputted bounds using For Loop

Posted 19 May 2015

blessingtz, on 19 May 2015 - 10:12 AM, said:

...Here is the word-for-word job: "The numbers generated from this function for values of x ranging from 1 to 20 are all prime. Write a program that displays each number generated for values from 1 to 20."

Any help is greatly appreciated!

According to this the function should use values from 1-20 inclusive. The result of the function will be greater than or equal to 41 by definition.

Here it is in code:
```        For numbers As Integer = 1 To 20
Debug.WriteLine(((numbers - 1) * numbers) + 41)
Next

```

BTW - I factored the formula.
2. In Topic: Incorrect divide by zero ?

Posted 5 May 2015

From Double:
"Using Floating-Point Numbers

When performing binary operations, if one of the operands is a Double, then the other operand is required to be an integral type or a floating-point type (Double or Single). Prior to performing the operation, if the other operand is not a Double, it is converted to Double, and the operation is performed using at least Double range and precision. If the operation produces a numeric result, the type of the result is Double.

The floating-point operators, including the assignment operators, do not throw exceptions. Instead, in exceptional situations the result of a floating-point operation is zero, infinity, or NaN, as described below:
•If the result of a floating-point operation is too small for the destination format, the result of the operation is zero.

•If the magnitude of the result of a floating-point operation is too large for the destination format, the result of the operation is PositiveInfinity or NegativeInfinity, as appropriate for the sign of the result.

•If a floating-point operation is invalid, the result of the operation is NaN.

•If one or both operands of a floating-point operation are NaN, the result of the operation is NaN."
3. In Topic: Incorrect divide by zero ?

Posted 5 May 2015

djjeavons, on 05 May 2015 - 06:32 AM, said:

targolo, on 05 May 2015 - 04:32 AM, said:

If you're right, all the other lines should also raise an exception...

The other statements do not throw an exception as 0 / 0 returns -1.#IND. Whereas, the line:

```Dim c As Decimal = 1
Console.WriteLine(IIf(b = 0, 0, c / B)/>/>)

```

throws the exception because c is 1D and therefore 1D / 0 will throw an exception.

As mentioned, use a standard If statement.

I agree. Either use a standard If or use the If Operator. If performance is a concern use the standard If.
4. In Topic: Finding Date for Next Monday

Posted 8 Feb 2015

I made a comment about performance earlier so I create a test to check the three differing versions. Again, performance is only a concern if this function is needed a lot.

Here is the code used to test:
```        Const tries As Integer = 100000
Dim testdts As New List(Of DateTime) From {#2/1/2015#, #2/2/2015#, #2/3/2015#, #2/4/2015#, #2/5/2015#, #2/6/2015#, #2/7/2015#}
Dim stpw As New Stopwatch

stpw.Reset()
stpw.Start()
For x As Integer = 1 To tries
For d As Integer = 0 To testdts.Count - 1
'uncomment one of the following three Dim statements
'
'Dim nxtMon As DateTime = nextMondayIF(testdts(d))
'Dim nxtMon As DateTime = nextMondayOneLine(testdts(d))
'Dim nxtMon As DateTime = nextMondayLoop(testdts(d))

'for checking results
'If x = 1 Then Debug.WriteLine("{0} {1} {2} {3}", d, d.DayOfWeek, nxtMon, nxtMon.DayOfWeek)
Next
Next
stpw.Stop()
Debug.WriteLine(stpw.ElapsedTicks.ToString("n0"))
Debug.WriteLine(stpw.ElapsedMilliseconds.ToString("n0"))

```

Here are the three next Monday methods:

```    Private Function nextMondayIF(d As DateTime) As DateTime
If d.DayOfWeek >= DayOfWeek.Monday Then 'is it mon - sat?
'yes
Else
'sun
End If
End Function

Private Function nextMondayOneLine(d As DateTime) As DateTime
Return d.AddDays(If(d.DayOfWeek = DayOfWeek.Monday, 7, (7 - d.DayOfWeek + 1) Mod 7))
End Function

Private Function nextMondayLoop(d As DateTime) As DateTime
Do
Loop Until d.DayOfWeek = DayOfWeek.Monday
Return d
End Function

```

The simple if and the one liner were close, and the loop took about twice as long as the other two.
5. In Topic: Finding Date for Next Monday

Posted 7 Feb 2015

andrewsw, on 07 February 2015 - 04:30 PM, said:

It is possible to use DayOfWeek:
```For x As Integer = 0 To 30
dim mon = day.AddDays((7 - day.DayOfWeek + 1) Mod 7)
Debug.Print(day & " " & mon)
Next x
```

This assumes that a Monday will return the same day, not the following Monday.

So the expression to get the following Monday is:
```Dim mon = day.AddDays((7 - day.DayOfWeek + 1) Mod 7)
```

I assumed next Monday was the next Monday even if it was Monday. If on the 9th of this month I told you something was going to happen next Monday I wouldn't mean the 9th. Would you think I did? Of course we don't know what the OP intends.

My Information

Member Title:
Age:
Age Unknown
Birthday:
July 29
Gender:
Years Programming:
45

E-mail:
Private
Website URL:
http://