rounding

Rounding to the Nearest Ending Digits

泄露秘密 提交于 2019-12-22 10:56:35
问题 I have the following function that rounds a number to the nearest number ending with the digits of $nearest , and I was wondering if there is a more elegant way of doing the same. /** * Rounds the number to the nearest digit(s). * * @param int $number * @param int $nearest * @return int */ function roundNearest($number, $nearest, $type = null) { $result = abs(intval($number)); $nearest = abs(intval($nearest)); if ($result <= $nearest) { $result = $nearest; } else { $ceil = $nearest - substr(

Ambiguous reference to member when using ceil or round

此生再无相见时 提交于 2019-12-22 10:30:32
问题 I am just trying to use the ceil or round functions in Swift but am getting a compile time error: Ambiguous reference to member 'ceil'. I have already imported the Foundation and UIKit modules. I have tried to compile it with and without the import statements but no luck. Does anyone one have any idea what I am doing wrong? my code is as follow; import UIKit @IBDesignable class LineGraphView: GraphView { override func setMaxYAxis() { self.maxYAxis = ceil(yAxisValue.maxElement()) } } 回答1: This

Add round corners to a jpeg file

假装没事ソ 提交于 2019-12-22 09:55:38
问题 I am trying to add round corners to a jpeg file, but the problem is that after adding round corners, I am getting a black background color. Somehow I am not able to change it to any other color (white, transparent, red). It just simply shows black background where the image has rounded corners. The code that I am using is: <?php $image = new Imagick('example.jpg'); $image->setBackgroundColor("red"); $image->setImageFormat("jpg"); $image->roundCorners(575,575); $image->writeImage("rounded.jpg"

How to round up a complex number?

五迷三道 提交于 2019-12-22 07:10:47
问题 How can I round up a complex number (e.g. 1.9999999999999998-2j ) as 2-2j ? When I tried using print(round(x,2)) it showed Traceback (most recent call last): File "C:\Python34\FFT.py", line 22, in <module> print(round(x,2)) TypeError: type complex doesn't define __round__ method 回答1: Round real part and imaginary part separately and combine them: >>> num = 1.9999999999999998-2j >>> round(num.real, 2) + round(num.imag, 2) * 1j (2-2j) 回答2: If all you want to do is represent the value rounded as

Rounding integers routine

◇◆丶佛笑我妖孽 提交于 2019-12-22 06:52:36
问题 There is something that baffles me with integer arithmetic in tutorials. To be precise, integer division. The seemingly preferred method is by casting the divisor into a float, then rounding the float to the nearest whole number, then cast that back into integer: #include <cmath> int round_divide_by_float_casting(int a, int b){ return (int) std::roundf( a / (float) b); } Yet this seems like scratching your left ear with your right hand. I use: int round_divide (int a, int b){ return a / b + a

How to round a number to within a certain range?

怎甘沉沦 提交于 2019-12-22 05:12:03
问题 I have a value like this: 421.18834 And I have to round it mathematical correctly with a mask which can look like this: 0.05 0.04 0.1 For example, if the mask is 0.04, i have to get the value 421.20 , because .18 is nearer at .20 than .16. All functions that I found using Google didn't work. Can you please help me? 回答1: double initial = 421.18834; double range = 0.04; int factor = Math.round(initial / range); // 10530 - will round to correct value double result = factor * range; // 421.20 回答2

Faster implementation of Math.round?

℡╲_俬逩灬. 提交于 2019-12-22 04:30:42
问题 Are there any drawbacks to this code, which appears to be a faster (and correct) version of java.lang.Math.round ? public static long round(double d) { if (d > 0) { return (long) (d + 0.5d); } else { return (long) (d - 0.5d); } } It takes advantage of the fact that, in Java, truncating to long rounds in to zero. 回答1: There are some special cases which the built in method handles, which your code does not handle. From the documentation: If the argument is NaN , the result is 0. If the argument

Rounding a float number in objective-c

一曲冷凌霜 提交于 2019-12-22 04:19:53
问题 I want to know if there is a simple function that I can use such this sample. I have a float value = 1.12345; I want to round it with calling something like float value2 = [roundFloat value:value decimal:3]; NSLog(@"value2 = %f", value2); And I get "1.123" Is there any Library or default function for that or I should write a code block for this type of calculations? thank for your help in advance 回答1: Using NSLog(@"%f", theFloat) always outputs six decimals, for example: float theFloat = 1;

Ruby: Round number down to nearest number based on arbitrary list of numbers

ⅰ亾dé卋堺 提交于 2019-12-22 04:11:46
问题 Say I have an array of integers: arr = [0,5,7,8,11,16] and I have another integer: n = 6 I need a function that rounds down to the nearest number from the array: foo(n) #=> 5 As you can see, the numbers do not have a fixed pattern. What's an elegant way to do this? Thanks 回答1: Use select followed by max: arr = [0,5,7,8,11,16] puts arr.select{|item| item < 6}.max Result: 5 This runs in linear time and doesn't require that the array is sorted. 回答2: If you are using relatively small arrays (and

Ruby: Round number down to nearest number based on arbitrary list of numbers

安稳与你 提交于 2019-12-22 04:11:15
问题 Say I have an array of integers: arr = [0,5,7,8,11,16] and I have another integer: n = 6 I need a function that rounds down to the nearest number from the array: foo(n) #=> 5 As you can see, the numbers do not have a fixed pattern. What's an elegant way to do this? Thanks 回答1: Use select followed by max: arr = [0,5,7,8,11,16] puts arr.select{|item| item < 6}.max Result: 5 This runs in linear time and doesn't require that the array is sorted. 回答2: If you are using relatively small arrays (and