locked
math function RRS feed

  • Question

  • User-1799376286 posted

    How do i write a function that take integer and return me the value such that if i pass: (I am using vb.net)


    35000 it should return 40000

    37000 it should return 40000

    27000 it should return 30000

    27200 it should return 30000

    22000 it should return 25000

    22500 it should return 25000

    Monday, July 26, 2010 11:48 AM

Answers

  • User-861155893 posted

    Just to really emphasise that using modulus works and to hopefully stop people giving more ridiculous answers to this simple solution I have provided a full c# console class, its output and some other info. I also fixed a bug and throw exception on division by zero


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleTest
    {
        class Program
        {
            static int[] testinputs = new int[] { -1, 0, 1, 5, 10 };
    
            private static int[] StepAmount = Enumerable.Range(1, 3).ToArray();
    
            static int result;
    
            static void Main(string[] args)
            {
                foreach (int testNum in testinputs)
                {
                    foreach (int stepAmount in StepAmount)
                    {
                        result = RoundNumberToStep(stepAmount, testNum);
                        Output(result, stepAmount, testNum);
                    }
                }
            }
    
            /// <summary>
            /// Actual function -
    /// </summary> /// <param name="roundStep"></param> /// <param name="input"></param> /// <returns></returns> public static int RoundNumberToStep(int roundStep, int input) { if (roundStep == 0) throw new NotImplementedException("Can't do steps of zero"); int a = input % roundStep; return a == 0 ? input : (roundStep - (input%roundStep)) + input; } static void Output(int result, int stepAmount, int testNum) { string msg = String.Format("Input:{0}\tStep:{1}\tResult:{2} [ % = {3} ]", testNum, stepAmount, result, testNum % stepAmount); System.Diagnostics.Debug.WriteLine(msg); } } }


    Output from method:

    Input:-1    Step:1    Result:-1 [ % = 0 ]
    Input:-1    Step:2    Result:2 [ % = -1 ]
    Input:-1    Step:3    Result:3 [ % = -1 ]
    Input:0    Step:1    Result:0 [ % = 0 ]
    Input:0    Step:2    Result:0 [ % = 0 ]
    Input:0    Step:3    Result:0 [ % = 0 ]
    Input:1    Step:1    Result:1 [ % = 0 ]
    Input:1    Step:2    Result:2 [ % = 1 ]
    Input:1    Step:3    Result:3 [ % = 1 ]
    Input:5    Step:1    Result:5 [ % = 0 ]
    Input:5    Step:2    Result:6 [ % = 1 ]
    Input:5    Step:3    Result:6 [ % = 2 ]
    Input:10    Step:1    Result:10 [ % = 0 ]
    Input:10    Step:2    Result:10 [ % = 0 ]
    Input:10    Step:3    Result:12 [ % = 1 ]

    ... etc upto 5000


    Notes:

    negative numbers go positive - theres conflicting info on how negatives and modulus are treated (at a Pure math level)...

    'bug fixed' was me just not testing for modulus = 0 (which indicated that the input was already 'rounded'

    so the final method is one defensive line for testing for 0 (which would throw indivisible zero exception) a test for the ternary and a ternary operator... I'm sure this could be trimmed down further




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 31, 2010 7:20 AM

All replies

  • User1790760290 posted

    function GetArrange(value as int) as int

    If value<25000 Then

    return 25000

    Else If value <30000 Then

    return 30000

    Else

    return 40000

    End If

    end function 

    Monday, July 26, 2010 2:29 PM
  • User1224194097 posted

    write a function like this

    Public Shared Function GetNextMultiple(Number As Integer, NextMultiple As Integer) As Integer
    	If NextMultiple = 0 Then
    		Return 0
    	End If
    	Dim add As Integer = NextMultiple / Math.Abs(NextMultiple)
    	Return ((Number + NextMultiple - add) / NextMultiple) * NextMultiple
    End Function
    Dim num As Integer = GetNextMultiple(35000, 5000)

    but if you pass more than 100000, this function will return incorrect value, you have to calculate the dividend based on the number of digits, if the number is in thousands use 5000 etc.,

    try that and see if it works for you


    Monday, July 26, 2010 2:43 PM
  • User-861155893 posted

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleTest
    {
        class Program
        {
            static int[] testinputs = new int[] { 22500, 32500, 3500, 35000, 37000, 37200  };
    
            static int RoundStep = 5000;
    
            static int result;
    
            static void Main(string[] args)
            {
                foreach (int testNum in testinputs)
                {
                    result = (RoundStep - (testNum % RoundStep)) + testNum;
                    System.Diagnostics.Debug.WriteLine("Input " + testNum + " rounded to " + result + " Modulo = " + (RoundStep - (testNum % RoundStep)));  
                }
            }
        }
    }



    This should work on any positive number... not tested on negatives. should work to positive infinity... ahh the wonder of modulus

    Monday, July 26, 2010 3:50 PM
  • User-1465674731 posted

    i think you have write logic on your own, here are some tips

    1) substring the number only and take out preceeding zeros (lets you have a variable n, in your example for 35000 just take 35 in the variable and remove zeros so n=35,)

    2) Once you got the actual digit other than zeros n= 35 then check what is first place digit, is it less than 5 if yes then again check if second place digit is less than 5 and is not the last digit in the number then replace it with 5 and make other previous digits to zero

    3) If first place digit is more than or equal to 5 and second place digit is more than 5 and is not the last digit then just make it zero and add one to the last digit.

    you need to code this algorithm and call it recursively.


    am i making sense?

    Monday, July 26, 2010 4:57 PM
  • User-861155893 posted

    Or you could just use modulus as per my example i.e.

            public static int RoundNumberToStep(int StepAmount, int input)
            {
                return (StepAmount - (input % StepAmount)) + input;
            }



    Monday, July 26, 2010 5:08 PM
  • User-660870441 posted

    How do i write a function that take integer and return me the value
     

    Hi,

    I wrote a function for you. Please have a try on it.

    ' This is the function you need, pass a value to it and it returns a result
    Public Shared Function Round(value As Decimal) As Integer
    	' change this value to 1000 or 100 if you want to modify this function
    	Dim multiple As Integer = 10000
    
    	value *= 2
    	value /= multiple
    	' let x.1 ~ x.8 be (x + 1) 
    	value = CInt(Math.Truncate(value - 1000)) + 1000
    	Return CInt(Math.Truncate(value * multiple)) \ 2
    End Function
    
    ' Test the function
    Private Shared Sub Main(args As String())
    	Dim inputs As Integer() = {35000, 37000, 27000, 27200, 22000, 22500}
    	For Each value As Integer In inputs
    		Console.WriteLine(Round(value))
    	Next
    	' The ouput is : 35000, 40000, 30000, 30000, 25000, 25000
    
    	Console.ReadKey()
    End Sub

    Sorry for the complex function.  But after I debugged it, it works.

    If I misunderstood you, please feel free to let me know.

    Friday, July 30, 2010 12:03 AM
  • User107841110 posted

    hgmnhgmhgmg

    Friday, July 30, 2010 1:04 AM
  • User-861155893 posted

    Just to really emphasise that using modulus works and to hopefully stop people giving more ridiculous answers to this simple solution I have provided a full c# console class, its output and some other info. I also fixed a bug and throw exception on division by zero


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleTest
    {
        class Program
        {
            static int[] testinputs = new int[] { -1, 0, 1, 5, 10 };
    
            private static int[] StepAmount = Enumerable.Range(1, 3).ToArray();
    
            static int result;
    
            static void Main(string[] args)
            {
                foreach (int testNum in testinputs)
                {
                    foreach (int stepAmount in StepAmount)
                    {
                        result = RoundNumberToStep(stepAmount, testNum);
                        Output(result, stepAmount, testNum);
                    }
                }
            }
    
            /// <summary>
            /// Actual function -
    /// </summary> /// <param name="roundStep"></param> /// <param name="input"></param> /// <returns></returns> public static int RoundNumberToStep(int roundStep, int input) { if (roundStep == 0) throw new NotImplementedException("Can't do steps of zero"); int a = input % roundStep; return a == 0 ? input : (roundStep - (input%roundStep)) + input; } static void Output(int result, int stepAmount, int testNum) { string msg = String.Format("Input:{0}\tStep:{1}\tResult:{2} [ % = {3} ]", testNum, stepAmount, result, testNum % stepAmount); System.Diagnostics.Debug.WriteLine(msg); } } }


    Output from method:

    Input:-1    Step:1    Result:-1 [ % = 0 ]
    Input:-1    Step:2    Result:2 [ % = -1 ]
    Input:-1    Step:3    Result:3 [ % = -1 ]
    Input:0    Step:1    Result:0 [ % = 0 ]
    Input:0    Step:2    Result:0 [ % = 0 ]
    Input:0    Step:3    Result:0 [ % = 0 ]
    Input:1    Step:1    Result:1 [ % = 0 ]
    Input:1    Step:2    Result:2 [ % = 1 ]
    Input:1    Step:3    Result:3 [ % = 1 ]
    Input:5    Step:1    Result:5 [ % = 0 ]
    Input:5    Step:2    Result:6 [ % = 1 ]
    Input:5    Step:3    Result:6 [ % = 2 ]
    Input:10    Step:1    Result:10 [ % = 0 ]
    Input:10    Step:2    Result:10 [ % = 0 ]
    Input:10    Step:3    Result:12 [ % = 1 ]

    ... etc upto 5000


    Notes:

    negative numbers go positive - theres conflicting info on how negatives and modulus are treated (at a Pure math level)...

    'bug fixed' was me just not testing for modulus = 0 (which indicated that the input was already 'rounded'

    so the final method is one defensive line for testing for 0 (which would throw indivisible zero exception) a test for the ternary and a ternary operator... I'm sure this could be trimmed down further




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 31, 2010 7:20 AM