round up to next multiple of 10 vb - vb.net

How can I round a value UP to the next multiple of 10 in VB ?
e.g.
19 -> 20
35 -> 40
21 -> 30
I found so things saying use a round function but when i type it in to my IDE (Microsoft Visual Studio Express 2015) it doesn't recognise it.
Thanks in advance

To round up use the ceiling function. (info here)
myNumber = Math.Ceiling(myNumber / 10) * 10
Deviding it first by 10 and then multiplying it again with 10 will do the trick.
Update: in case you are wondering, there are no problems with Integer

This is an Integer-based solution.
myNumber = If(myNumber Mod 10 = 0, myNumber, If(myNumber > 0, 10, 0) + 10 * (myNumber \ 10))
It doesn't suffer from rounding and is also is at least 2x faster than using Math.Ceiling.

Related

Round function VBA EXCEL

I need simulate classic play cube (values 1-6) by Rnd function. My solution is Round(5 * Rnd() + 1, 0) , but when I generate 1 milion values, I have not good results (by picture).
I think, that problem is in bad use of ROUND function, but I do not know where.
Thank you for your answers...
Let's look at this piece-by-piece.
Rnd() gives a number between 0 and 1.
So 5 * Rnd() + 1 gives a number between 1 and 6.
We can think of this number as being equally likely to be in one of 10 half-integer areas: [1,1.5], [1.5,2]...[5.5,6].
One of these will round to 1 - [1,1.5].
Two of these will round to 2 - [1.5,2] and [2,2.5].
3, 4 and 5 similarly have two of these which will round to them.
One of these will round to 6 - [5.5,6]
This explains the distribution you're seeing.
Therefore Round(6 * Rnd() + 0.5, 0) should give you what you're looking for as it extends the area by half in both directions, so gives 1 and 6 the correct weightings.
Have you tried randbetween() ?? worked fine for me...

How fast calculate MODULUS for divisor greater than 8, 16, 32

I´m trying to make very fast MOD operations.
I saw in several pages we can do alternatively calculate the MOD using the AND operator with (Divisor-1). Eg.:
result = (100 mod 8) is the same as
result = (100 and 7)
It functions perfectly if the divisor is less than 8 bits, but if we calculate (1245 mod 67) we can see the result is different of (1245 and 66).
So, how can I calculate this faster than using the MOD operator provided by VB.NET language?
Thanks!
Using a bitwise AND only works for modulus powers of 2 (and only positive ones, at that). It does not work for other numbers. See this link
I think that the modulus operator built into the framework is fast and you probably won't be able to improve on it.
Well, 100 mod 8 = 100 and 7 works, because 7 is binary 111b.
Whatever number you have (100 decimal is 1100100b), the last 3 binary digits (bits) are kept due to the 111b. For 100, that would be 100b = 4.
Now consider what you did attempt with 1245 mod 67.
1245 is binary 10011011101b.
67 is binary 1000011b.
Can you see, why it's not working?

Round to nearest 10 multiplication

I have a bit of math question for my sql code. I want to be able to work out the following in my code using a mathematical formula, if anyone knows how I would love to know.
Any number 1->99 : 10
Any number 100->999 : 100
Any number 1000->9999 : 1000
...
Is there anyway to work out the 10 multiplier just from the value? I feel like this should be an easy formula but I cant seem to get it.
Thanks
How about this?
SELECT POWER(10, CONVERT(INT, LOG10(#Input)))
It takes the log base 10 of the input value (which returns the value of the exponent to which you would have to raise 10 to in order to get the input value), then it lops off the decimal portion leaving only the whole number, and then raises 10 to that power.
You just need logs and their opposite (power)...
power(10, floor(log10(x)))
As follows...
log10(99) = 1.9956351946
floor(1.9956351946) = 1
power(10, 1) = 10
This does, however, assume that your example is wrong and that 1 -> 9 should "round" to 1...
log10(9) = 0.95424250943
floor(0.95424250943) = 0
power(10, 0) = 1

VB.NET doesn't round numbers correctly?

I'm testing the speed of some functions so I made a test to run the functions over and over again and I stored the results in an array. I needed them to be sorted by the size of the array I randomly generated. I generate 100 elements. Merge sort to the rescue! I used this link to get me started.
The section of code I'm focusing on:
private void mergesort(int low, int high) {
// check if low is smaller then high, if not then the array is sorted
if (low < high) {
// Get the index of the element which is in the middle
int middle = low + (high - low) / 2;
// Sort the left side of the array
mergesort(low, middle);
// Sort the right side of the array
mergesort(middle + 1, high);
// Combine them both
merge(low, middle, high);
}
}
which translated to VB.NET is
private sub mergesort(low as integer, high as integer)
' check if low is smaller then high, if not then the array is sorted
if (low < high)
' Get the index of the element which is in the middle
dim middle as integer = low + (high - low) / 2
' Sort the left side of the array
mergesort(low, middle)
' Sort the right side of the array
mergesort(middle + 1, high)
' Combine them both
merge(low, middle, high)
end if
end sub
Of more importance the LOC that only matters to this question is
dim middle as integer = low + (high - low) / 2
In case you wanna see how merge sort is gonna run this baby
high low high low
100 0 10 0
50 0 6 4
25 0 5 4
12 0 12 7
6 0 10 7
3 0 8 7
2 0 :stackoverflow error:
The error comes from the fact 7 + (8 - 7) / 2 = 8. You'll see 7 and 8 get passed in to mergesort(low, middle) and then we infinite loop. Now earlier in the sort you see a comparison like this again. At 5 and 4. 4 + (5 - 4) / 2 = 4. So essentially for 5 and 4 it becomes 4 + (1) / 2 = 4.5 = 4. For 8 and 7 though it's 7 + (1) / 2 = 7.5 = 8. Remember the numbers are typecasted to an int.
Maybe I'm just using a bad implementation of it or my typecasting is wrong, but my question is: Shouldn't this be a red flag signaling something isn't right with the rounding that's occuring?
Without understanding the whole algorithm, note that VB.NET / is different than C# /. The latter has integer division by default, if you want to truncate decimal places also in VB.NET you have to use \.
Read: \ Operator
So i think that this is what you want:
Dim middle as Int32 = low + (high - low) \ 2
You are correct in your diagnosis: there's something inconsistent with the rounding that's occurring, but this is entirely expected if you know where to look.
From the VB.NET documentation on the / operator:
Divides two numbers and returns a floating-point result.
This documentation explicitly states that , if x and y are integral types, x / y returns a Double. So, 5 / 2 in VB.NET would be expected to be 2.5.
From the C# documentation on the / operator:
All numeric types have predefined division operators.
And further down the page:
When you divide two integers, the result is always an integer.
In the case of C#, if x and y are integers, x / y returns an integer (rounded down). 5 / 2 in C# is expected to return 2.

How would I do this in a program? Math question

I'm trying to make a generic equation which converts a value. Here are some examples.
9,873,912 -> 9,900,000
125,930 -> 126,000
2,345 -> 2,400
280 -> 300
28 -> 30
In general, x -> n
Basically, I'm making a graph and I want to make values look nicer. If it's a 6 digit number or higher, there should be at least 3 zeros. If it's a 4 digit number or less, there should be at least 2 digit numbers, except if it's a 2 digit number, 1 zero is fine.
(Ignore the commas. They are just there to help read the examples). Anyways, I want to convert a value x to this new value n. What is an equation g(x) which spits out n?
It is for an objective-c program (iPhone app).
Divide, truncate and multiply.
10**x * int(n / 10**(x-d))
What is "x"? In your examples it's about int(log10(n))-1.
What is "d"? That's the number of significant digits. 2 or 3.
Ahhh rounding is a bit awkward in programming in general. What I would suggest is dividing by the power of ten, int cast and multiplying back. Not remarkably efficient but it will work. There may be a library that can do this in Objective-C but that I do not know.
if ( x is > 99999 ) {
x = ((int)x / 1000) * 1000;
}
else if ( x > 999 ) {
x = ((int) x / 100) * 100;
}
else if ( x > 9 ) {
x = ((int) x / 10) * 10;
}
Use standard C functions like round() or roundf()... try man round at a command line, there are several different options depending on the data type. You'll probably want to scale the values first by dividing by an appropriate number and then multiplying the result by the same number, something like:
int roundedValue = round(someNumber/scalingFactor) * scalingFactor;