Round up in XPath 1.0 - xpath

I'm dealing with a system where a number that is (even barely) over whole should be rounded up to the next-larger whole number. So
<Number>1.2</Number>
<Number>1.01</Number>
<Number>2</Number>
<Number>1.9183</Number>
should all be rounded to 2. How can I achieve this?

You can use the ceiling() function. It takes a number as an argument and it will replace the number by the next integer which is larger than the number.
In XPath you also have the round() function which replaces the number by its nearest integer (larger or smaller), and floor() which is the opposite of ceiling().
See: XPath Specification, 4.4 Number Functions

Related

how to get the integer part of a real number that is positive using only basic arithmetic

This is a question for school, reinventing the wheel as usual.
I'm allowed to use basic arithmetic +, -, *, / and comparison, but I'm obviously not allowed to use cast.
The method has to be efficient, so I thought about multiplying a variable by 2 until it's bigger then do a dichomitic search between the powers of 2 that contains the real number I want to extract the integer part.
However, in the next section, I'm not allowed to use these basic arithmetic and comparison between integer and float, only between 2 integers, or 2 floats.
I can't find any solution to this...
You can follow your idea of multiplication by two to surpass the value then dichomitic search (aka binary search) to get the desired integer. However, since you are not allowed to compare a float with an integer, start with two values, the float 1.0 and the integer 1. Do all your multiplications and comparisons with the float value, then at each step whatever you do to the float value you also do to the integer value. So at any point, your float value and your integer value are equal, and you are using the float value for all comparisons with your given value.
So if your given value is 3.1416, you start with your initial guess values of 1.0 and 1. 1.0 is less than 3.1416, so you double both guesses and get 2.0 and 2. The float 2.0 is still less than 3.1416 so you double both guesses again and get 4.0 and 4. Your float guess 4.0 is finally too high, so you use binary search and try 3.0 and 3. The float guess is low. However, your integer guess 3 is just one away from your previous integer guess of 4, so you are done. The final integer result is thus 3.

Equivalent "chop" function in Maple

The Chop feature in Mathematica replaces approximate real numbers in expression that are close to zero by the exact integer. I am searching for an equivalent function in Maple. Kindly note that I am NOT in search of trunc, round, frac etc. as they round off other floats to integers. I am only interested in "chopping" or rounding the near to 0 numbers to 0.
Any help is highly appreciated!
Check out Maple's command fnormal.

Easiest way to do VHDL floating point division?

I'm looking for easiest way to divide two floating point numbers using VHDL. I need the code to be synthesizable (I'll be implementing it on Spartan 3 FPGA).
First operand will always be a fixed number (e.g. 600), and second one will be integer, let's say between 0 and 99999. Fixed number is dividend, and the integer one is divisor. So I'll have to calculate something like this: 600/124.
Or any other number instead of 124, of course that is in range between 0 and 99999. Second number (the one that is changing) will always be integer !! (there won't be something like 123.45).
After division, I need to convert the result into integer (round it up or just ignore numbers after decimal point, which ever is faster).
Any ideas ? Thanks !
There are many ways to do this, with the easiest being a ROM. You don't need floating point anywhere since doing an integer divide and compensating for a non-zero remainder can give you the same results. I'd suggest calculating the first 600 results in MATLAB or a spreadsheet so you can see that handling values up to 99999 isn't necessary.
Also, some common nomenclature for range and precision is QI.F where I is the number of integer bits and F is the number of fractional bits. Thus 0..99999 would be Q17.0 and your output would be Q10.0.
There's an FP divide function in this VHDL file from this site.

Get a pseudorandom float number in a closed interval [a, b]

I would like to know the best way to get a pseudorandom float number in a closed interval using the Ruby rand kernel function (please not Random module).
To take an example I will use the closed interval [0.0, 7.7] (both 0.0 and 7.7 included in the interval), but any other float interval should be valid too.
For the interval [0.0, 7.7] the next solution is not valid:
rand * 7.7
Why?
If you call rand without arguments you will get a pseudorandom floating point number greater than or equal to 0.0 and less than 1.0. So what is the range of float numbers that the previous solutions can give to us?
rand will return a pseudorandom float number in the range [0.0, 0.9999999...]
0.0 * 7.7
=> 0.0 # Correct!
0.9999999 * 7.7
=> 7.69999923 # Incorrect!
The interval does not match with [0.0, 7.7].
Does anyone know an elegant solution to this problem?
Thank you!
There's a Random class that can do what you want:
generator = Random.new # You need to instance it
generator.rand 0.0..7.7
(The documentation states the difference between 0.0..7.7 and 0.0...7.7 will be taken in account.)
In the future 1.9.3, you'll be able to pass a range to Kernel#rand and Random.rand (you can already do that in the preview version).
I would do something like this:
Fineness = 2**64
puts rand(Fineness+1)*7.7/Fineness
Whenever rand returns its maximum possible value, you will get Fineness*7.7/Fineness which turns out to equal 7.7 exactly (but I'm not totally sure this will always be the case, because floats are inexact).
As long as Fineness has more bits in it than a double on your computer, then I believe you will not notice any strangeness in the distribution of your results.
How about:
(rand/0.9999999999999999...)*7.7
Basically, normalize the random number by the largest possible random number. That way you will create the range [0..1].
However, I am unsure how to get the max number, which is less than 1.0 in ruby.
Why do you need this? I don't know of a case where there would be a need for this as a true single or double precision number. On the other hand, there are real cases where you might need numbers between 0.0 and 7.7 in increments of 0.1. In that case you could use well established techniques to go from 0 to 77 and then divide by 10.
Depending on the number of digits of precision you need you could use a round to even approach to snap to the boundaries of the interval to the edges. Hope this helps.
Here is the text from Wikipedia
Round half to evenA tie-breaking rule that is even less biased is
round half to even, namely
If the fraction of y is 0.5, then q is the even integer nearest to y.
Thus, for example, +23.5 becomes +24, +22.5 becomes +22, −22.5 becomes
−22, and −23.5 becomes −24.
This method also treats positive and negative values symmetrically,
and therefore is free of overall bias if the original numbers are
positive or negative with equal probability. In addition, for most
reasonable distributions of y values, the expected (average) value of
the rounded numbers is essentially the same as that of the original
numbers, even if the latter are all positive (or all negative).
However, this rule will still introduce a positive bias for even
numbers (including zero), and a negative bias for the odd ones.
This variant of the round-to-nearest method is also called unbiased
rounding (ambiguously, and a bit abusively), convergent rounding,
statistician's rounding, Dutch rounding, Gaussian rounding, or
bankers' rounding. This is widely used in bookkeeping.
This is the default rounding mode used in IEEE 754 computing functions
and operators.

How to convert a floating-point type to an integer type without rounding in VB6

What is the recommended way to convert a floating point type to an integer type, truncating everything after the decimal point? CLng rounds, apparently, and the documentation for the = operator doesn't mention the subject.
UseFix or Int depending on the treatment you wish for negative numbers.
Microsoft article Q196652 discusses rounding in incredible detail. Here is an excerpt
The VB Fix() function is an example
of truncation. For example, Fix(3.5)
is 3, and Fix(-3.5) is -3.
The Int() function rounds down to
the highest integer less than the
value. Both Int() and Fix() act
the same way with positive numbers -
truncating - but give different
results for negative numbers:
Int(-3.5) gives -4.
Full disclosure: I referred to this nice answer by elo80ka
see
this
Undocumented behavior of the CInt() function
The CInt() function rounds to the nearest integer value. In other words, CInt(2.4) returns 2, and CInt(2.6) returns 3.
This function exhibits an under-documented behavior when the fractional part is equal to 0.5. In this case, this function rounds down if the integer portion of the argument is even, but it rounds up if the integer portion is an odd number. For example, CInt(2.5) returns 2, but CInt(3.5) returns 4.
This behavior shouldn't be considered as a bug, because it helps not to introduce errors when doing statistical calculations. UPDATE: Matthew Wills let us know that this behavior is indeed documented in VB6's help file: When the fractional part is exactly 0.5, CInt and CLng always round it to the nearest even number. For example, 0.5 rounds to 0, and 1.5 rounds to 2. CInt and CLng differ from the Fix and Int functions, which truncate, rather than round, the fractional part of a number. Also, Fix and Int always return a value of the same type as is passed in.
For positive numbers you would use
truncated = Int(value)
If used on negative numbers it would go down, i.e. -7.2 would become -8.

Resources