logo
down
shadow

Round float to closer (or closest one)


Round float to closer (or closest one)

By : Snix
Date : November 21 2020, 09:01 AM
like below fixes the issue I have some "reference" float numbers (1.5, 3 and 7.1 for example), is there any way how to get some other "random" float rounded to closest one from the brackets? (I mean if there's something like round, which can takes reference values for rounding) , A bit more optimum answers:
the "for i" way, easier to read
code :
private static double roundForI(double toRound, double[] numbers) {
    int minIndex = 0;
    double minDiff = Double.MAX_VALUE;
    double diff;
    for (int i=0; i<numbers.length; ++i) {
        diff = Math.abs(numbers[i]-toRound);
        if (diff < minDiff) {
            minDiff = diff;
            minIndex = i;
        }
    }
    return numbers[minIndex];
}
private static double roundForEach(double toRound, double[] numbers) {
    double diff, roundValue = 0, minDiff = Double.MAX_VALUE;
    for (double dob : numbers) {
        if ((diff = Math.abs(dob-toRound)) < minDiff) {
            minDiff = diff;
            roundValue = dob;
        }
    }
    return roundValue;
}
private static double roundLambda(double n, double[] a) {
    double[] r = {0}, d = {Double.MAX_VALUE};
    Arrays.stream(a).filter((i)->Math.abs(i-n)<d[0]).forEach(i->{d[0]=Math.abs(i-n);r[0]=i;});
    return r[0];
}


Share : facebook icon twitter icon
Round to closest integer or closest .5 in Ruby

Round to closest integer or closest .5 in Ruby


By : lalala228
Date : March 29 2020, 07:55 AM
Any of those help Maybe the title is confusing, but I will try to explain with examples: , Multiply by 2, round, divide by 2.
code :
[4.12, 4.24, 4.25, 4.33, 4.53, 4.65, 4.75, 4.84].map do |x|
  r = (x * 2).round / 2.0
  r.to_i == r ? r.to_i : r
end

=> [4, 4, 4.5, 4.5, 4.5, 4.5, 5, 5]
Round a double to the closest and greater float

Round a double to the closest and greater float


By : fizzyprogramming
Date : March 29 2020, 07:55 AM
around this issue Simply assigning a double to float and back should tell, if the float is larger. If it's not, one should simply increment the float by one unit. (for positive floats). If this doesn't still produce expected result, then the double is larger than supported by a float, in which case float should be assigned to Inf.
code :
float next(double a) {
    float b=a;
    if ((double)b > a) return b;
    return std::nextafter(b, std::numeric_limits<float>::infinity());
}
float next_after(float a) {
    *(int*)&a += a < 0 ? -1 : 1;
    return a;
}
float next_after(float a) {
   union { float a; int b; } c = { .a = a };
   c.b += a < 0 ? -1 : 1;
   return c.a;
}
Function to round integer or float up to closest increment in Powershell

Function to round integer or float up to closest increment in Powershell


By : Hansdo
Date : March 29 2020, 07:55 AM
should help you out I want to create a function that rounds a number up to the closest increment of five in Powershell. Every number less than 1 should be rounded up to one, but every number greater than 1 should be rounded up to the closes increment of 5 (5, 10, 15 etc.). , Maybe this is more performant ( need to be tested, I've no time now):
code :
Function Get-Increment([float] $value, [int] $increment=5){    
    if($value -gt 1)
    {
      [Math]::Ceiling($value / $increment) * $increment;
    }
    else
    {
      [math]::Ceiling($value)    
    }    
}
How to divide unsigned integer of 256 and round to the closer value?

How to divide unsigned integer of 256 and round to the closer value?


By : mahsa
Date : March 29 2020, 07:55 AM
Any of those help I need to divide number of 256 and round it to closer value, for example, if input 255 I want to get 1, not 0. Now I'm using , Use this:
code :
unsigned int x = 150;
unsigned int z = (x + 128) >> 8;
int x = -150;
int z = (x >= 0 ? (x + 128) : (x - 128)) / 256;
How to remove all elements from sorted Ruby array which are closer to its closest neighbour than a limit?

How to remove all elements from sorted Ruby array which are closer to its closest neighbour than a limit?


By : Serrano Santiago
Date : March 29 2020, 07:55 AM
This might help you There seems to be some ambiguity in the question. I interpret it as I stated in a comment on the question.
code :
data = [ 3.42,  5.49,  6.12,  6.48,  7.11,  8.79,  9.36,
         9.54, 10.86, 10.95, 11.07, 13.08, 14.41, 14.92] 
limit = 0.5

([-Float::INFINITY].concat(data) << Float::INFINITY).each_cons(3).
  select { |a,b,c| b-a >= 0.5 && c-b >= 0.5 }.
  map { |_,b,_| b }
  #=> [3.42, 5.49, 7.11, 8.79, 14.41, 14.92]
Related Posts Related Posts :
  • How to query using objectify in Google App Engine app
  • List value is printing address instead of value
  • StringUtils.isBlank return false for null Strings
  • Sent parameter through PATCH android
  • i am not able to clear the code below given using throwable concept in java
  • How to get System time instead of Server time in java?
  • How to push json data into an arrya object in MONGO DB by java
  • Saving RelationshipEntity not working
  • Error on "instanceOf" when checking a subClass
  • Java - Splitting a image into 4 images
  • "Pinging" a server in Java
  • RoboGuice Proguard CreationException
  • Android Array list returns Null after adding values
  • Jar Executable what am I doing wrong?
  • How do I mitigate Connection leak triggered for connection com.mysql.jdbc.JDBC4Connection@11d08960,
  • How do i iterate values from a 2d array using conditions
  • Hide Button in CustomPagerAdapter
  • Java GC: does it ever make sense to call super.finalize() in a class that only extends Object?
  • 64bit HashMap in Java
  • Hibernate wildcard query on two columns
  • Want to Generate Auto Login Program Using Java
  • Spring security Oauth 2 with ajax login and form login
  • Parse findInBackground doesn't add to global arraylist?
  • Remove random item from ArrayList
  • in SpringMVC getMessage from i18n properties in a function outside the controller
  • How do I add JCheckBox next to each name that has been entered into an arraylist from a JTextField?
  • Return node ID along with complete node
  • InvocationTargetException. Cannot cast class X to class X. When invoked in Scala Imain through spark-submit
  • how to print elements of a 1D array into a N x N box
  • How to map csv file to pojo class in java
  • How, in Java, to encode default vhost %2F in URL/URI so it doesn't get changed to slash and fail to work
  • Tomcat 5.5 https configuration in Windows Server 2008 64bit
  • Enabling FIPS 140-2 compliance mode in MULE ESB
  • How to pass String PackageName to PkgName under parseXML() and run XMLParser class as a new activity when click on list
  • Error while creating spring -hibernate configuration file
  • Hand off control between java threads
  • NoClassDefFoundError: scala/collection/Seq
  • Getting stack overflow error on one machine but not the other on the same code
  • How can I loop AsyncAjaxRequest in GWT?
  • Failed to find provider info for urbanairship.provider
  • Android ListView Adapter Crash issue/Duplicates
  • Displaying Splash screen only once on user preference
  • Loop List<WebElement> doesn't stop despite no more WebElements
  • Check if pattern exists in a String
  • Swing - Key Problems (Arrow Keys, Tab, etc.) in GUIs with TextComponents
  • Why the solution for binary search tree validity is not working?
  • Double encrypting 2048 RSA?
  • create junit test for runnable class
  • Unable to read and sort information from a text file in Java
  • My device isn't getting detected in android studio (driver issue)
  • Representing a Graph in Java
  • How to XML mapping using jxls
  • Quick Sort stackoverflow error for large arrays
  • Parsing Apache Tika XML Output returns Unknown Tag
  • java simple UI loop, can't find bug
  • how to use greater than and less than in a single if statement in java
  • Error:(27, 13) Failed to resolve: com.antonyt.infiniteviewpager:library:1.0.0
  • Gradle dependencies on source set of different projects
  • Session Scope in Restful Spring Services
  • Java: Change a value in an overridden method of a different class to a value that was calculated in the class
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co