In Z notation, how to define a division operation for integer numbers - z-notation

I was wondering to know if anyone out here even used the "Z notation" in a professional environment.
Just curious to know some commonly-known applications of Z or your application.
For those who are not familiar : http://staff.washington.edu/jon/z/z-examples.html
I want to know "In Z notation, how to define a division operation for integer numbers"

The integer division is already part of Z's mathematical toolkit:
a \div b
(See the Z manual by M. Spivey, chapter 4.4 Numbers and Finiteness)

Related

Ceil(-0.24) prints -0 rather than just 0 in c++ [duplicate]

Consider the following C++ code:
double someZero = 0;
std::cout << 0 - someZero << '\n'; // prints 0
std::cout << -someZero << std::endl; // prints -0
The question arises: what is negative zero good for, and should it be defensively avoided (i.e. use subtraction instead of smacking a minus onto a variable)?
From Wikipedia:
It is claimed that the inclusion of signed zero in IEEE 754 makes it much easier to achieve numerical accuracy in some critical problems[1], in particular when computing with complex elementary functions[2].
The first reference is "Branch Cuts for Complex Elementary Functions or Much Ado About Nothing's Sign Bit" by W. Kahan, that is available for download here.
One example from that paper is 1/(+0) vs 1/(-0). Here, the sign of zero makes a huge difference, since the first expression equals +inf and the second, -inf.
In addition
Signed Zero Good For :
The zeroes can be considered as a variant of the extended real number line such that 1/−0 = −∞ and 1/+0 = +∞, division by zero is only undefined for ±0/±0.
Negatively signed zero echoes the mathematical analysis concept of approaching 0 from below as a one-sided limit, which may be denoted by x → 0−, x → 0−, or x → ↑0. The notation "−0" may be used informally to denote a small negative number that has been rounded to zero. The concept of negative zero also has some theoretical applications in statistical mechanics and other disciplines
There are only two real use-cases that I can see:
You want to show that a value is negative but very very small (perhaps infinitessimal), i.e. too small to represent as a float or double.
You are working with math that only allows negatives, but still want to display zero. There are a few cases in physics, complex numbers and number theory where this can be useful.
For the mostpart, it's not useful and should be avoided.
You may also want to take a look at this question: Is there a negative zero? and the IEEE 754 spec for floating point.
I'm making a measuring app, and the -0 is very useful for mixed numbers (such as separating into feet and inches).
Imagine that we have a variable "length" that we're trying to separate into "feet" and "inches".
(This is java code, but the same idea is true for C++).
feet = Math.signum(length) * Math.floor(Math.abs(length / 12));
// could also do feet = length>0 ? Math.floor(length / 12) : Math.ceil(length / 12)
inches = Math.abs(length) % 12;
If the length is between -1 feet and 0 feet, we'd want it to say -0 for the feet so we know it's negative.
Negative zero has for example some use when handling complex numbers...
In everyday use one should mostly avoid the negative zero.
Some links with information regarding background/uses/pitfalls of "negative zero":
http://en.wikipedia.org/wiki/Signed_zero
http://en.wikipedia.org/wiki/Floating_point#Signed_zero
http://en.wikipedia.org/wiki/Branch_cut
http://connect.microsoft.com/VisualStudio/feedback/details/344366/negative-zero-behavior-between-c-and-c-code-is-different
http://connect.microsoft.com/VisualStudio/feedback/details/292276/in-vs2005-c-zero-reported-as-negative-zero-for-double-type
C++ ceil and negative zero

Significance of Signed Zero [duplicate]

Consider the following C++ code:
double someZero = 0;
std::cout << 0 - someZero << '\n'; // prints 0
std::cout << -someZero << std::endl; // prints -0
The question arises: what is negative zero good for, and should it be defensively avoided (i.e. use subtraction instead of smacking a minus onto a variable)?
From Wikipedia:
It is claimed that the inclusion of signed zero in IEEE 754 makes it much easier to achieve numerical accuracy in some critical problems[1], in particular when computing with complex elementary functions[2].
The first reference is "Branch Cuts for Complex Elementary Functions or Much Ado About Nothing's Sign Bit" by W. Kahan, that is available for download here.
One example from that paper is 1/(+0) vs 1/(-0). Here, the sign of zero makes a huge difference, since the first expression equals +inf and the second, -inf.
In addition
Signed Zero Good For :
The zeroes can be considered as a variant of the extended real number line such that 1/−0 = −∞ and 1/+0 = +∞, division by zero is only undefined for ±0/±0.
Negatively signed zero echoes the mathematical analysis concept of approaching 0 from below as a one-sided limit, which may be denoted by x → 0−, x → 0−, or x → ↑0. The notation "−0" may be used informally to denote a small negative number that has been rounded to zero. The concept of negative zero also has some theoretical applications in statistical mechanics and other disciplines
There are only two real use-cases that I can see:
You want to show that a value is negative but very very small (perhaps infinitessimal), i.e. too small to represent as a float or double.
You are working with math that only allows negatives, but still want to display zero. There are a few cases in physics, complex numbers and number theory where this can be useful.
For the mostpart, it's not useful and should be avoided.
You may also want to take a look at this question: Is there a negative zero? and the IEEE 754 spec for floating point.
I'm making a measuring app, and the -0 is very useful for mixed numbers (such as separating into feet and inches).
Imagine that we have a variable "length" that we're trying to separate into "feet" and "inches".
(This is java code, but the same idea is true for C++).
feet = Math.signum(length) * Math.floor(Math.abs(length / 12));
// could also do feet = length>0 ? Math.floor(length / 12) : Math.ceil(length / 12)
inches = Math.abs(length) % 12;
If the length is between -1 feet and 0 feet, we'd want it to say -0 for the feet so we know it's negative.
Negative zero has for example some use when handling complex numbers...
In everyday use one should mostly avoid the negative zero.
Some links with information regarding background/uses/pitfalls of "negative zero":
http://en.wikipedia.org/wiki/Signed_zero
http://en.wikipedia.org/wiki/Floating_point#Signed_zero
http://en.wikipedia.org/wiki/Branch_cut
http://connect.microsoft.com/VisualStudio/feedback/details/344366/negative-zero-behavior-between-c-and-c-code-is-different
http://connect.microsoft.com/VisualStudio/feedback/details/292276/in-vs2005-c-zero-reported-as-negative-zero-for-double-type
C++ ceil and negative zero

Does modulus use floating point behind the scenes?

In C++ does a modulus use any floating point math behind the scenes?
int x = 1234;
int y = 5678;
int z = y % x; // any floating point used underneath to calculate the integer result?
As background, I was thinking about this question where he said he couldn't use any floating point without FP emulation. Then I realized that I wasn't sure if the modulus operator used any sort of floating point assembly operations. My guess is it does not, but I would like to be sure.
No1. Refer to some implementations of such an operator.
Assembly Language - How to Do Modulo? - "[In x86] the DIV instruction [..] gives both the quotient and remainder"
Assembly mod algorithm on processor with no division operator
1 An implementation can do whatever it wants insofar as the observed behavior is within the specification. However, I don't know of any implementation which would choose to use floating point operations, nor can I think of a general justification for doing so.
No it does not use the floating point arithmetic. The result can be obtained very simply
z = y - ( y/x ) * x;
Early computers sometimes have no floating point coprocessor. So such operations are performed by using machine commands that operate with integer numbers.
It's up to the implementation how "C++" calculates the modulus "behind the scenes"

Are the "reals" in Fortran the same as "floats" in C++?

I have translated some code from Fortran to C++ and both codes give me the same result for a given input with the exception of two data points in the middle of my data set.
My code calculates the distance between points and does some interesting things with that information. Two points in the C++ code are found to be at one distance from each other and at a different distance in Fortran. The code is long, so I won't post it.
This strikes me as weird because the two "strange points" are right in the middle of my code, whereas all of the other 106 points behave the same.
I have already read the Goldberg paper, and it makes me believe that real and float ought to be the same on my 32-bit system.
A real in Fortran may be float (which is kind 4) or double (kind 8) in C++.
It also may depend on your compiler options (i.e. math extensions, optimization, square root implementation, etc).
In most C/C++ implementations you'll encounter, float corresponds to REAL*4, and double corresponds to REAL*8.
This StackOverflow answer (and related comments) describe Fortran 90's types: Fortran 90 kind parameter.
Differences in floating point computations may arise due to different evaluation order. Floating point arithmetic is very sensitive to evaluation order, especially where addition and subtraction among values with a wide dynamic range is involved.
Also, C/C++ math and math libraries default to double precision in many surprising places, unless you explicitly ask for a float. For example, the constant 1.0 is a double precision constant. Consider the following code fragment:
float x;
x = 1.0 + 2.0;
x = x + 3.0;
The expression 1.0 + 2.0 is computed at double precision, and the result cast back to single precision. Likewise, the second statement x + 3.0 promotes x to double, does the arithmetic, and then casts back to float.
To get single precision constants and keep your arithmetic at single precision, you need to add the suffix f, as follows:
float x;
x = 1.0f + 2.0f;
x = x + 3.0f;
Now this arithmetic will all be done at single precision.
For math library calls, the single-precision variant also usually has an f suffix, such as cosf or sqrtf.

Uses for negative zero floating point value?

Consider the following C++ code:
double someZero = 0;
std::cout << 0 - someZero << '\n'; // prints 0
std::cout << -someZero << std::endl; // prints -0
The question arises: what is negative zero good for, and should it be defensively avoided (i.e. use subtraction instead of smacking a minus onto a variable)?
From Wikipedia:
It is claimed that the inclusion of signed zero in IEEE 754 makes it much easier to achieve numerical accuracy in some critical problems[1], in particular when computing with complex elementary functions[2].
The first reference is "Branch Cuts for Complex Elementary Functions or Much Ado About Nothing's Sign Bit" by W. Kahan, that is available for download here.
One example from that paper is 1/(+0) vs 1/(-0). Here, the sign of zero makes a huge difference, since the first expression equals +inf and the second, -inf.
In addition
Signed Zero Good For :
The zeroes can be considered as a variant of the extended real number line such that 1/−0 = −∞ and 1/+0 = +∞, division by zero is only undefined for ±0/±0.
Negatively signed zero echoes the mathematical analysis concept of approaching 0 from below as a one-sided limit, which may be denoted by x → 0−, x → 0−, or x → ↑0. The notation "−0" may be used informally to denote a small negative number that has been rounded to zero. The concept of negative zero also has some theoretical applications in statistical mechanics and other disciplines
There are only two real use-cases that I can see:
You want to show that a value is negative but very very small (perhaps infinitessimal), i.e. too small to represent as a float or double.
You are working with math that only allows negatives, but still want to display zero. There are a few cases in physics, complex numbers and number theory where this can be useful.
For the mostpart, it's not useful and should be avoided.
You may also want to take a look at this question: Is there a negative zero? and the IEEE 754 spec for floating point.
I'm making a measuring app, and the -0 is very useful for mixed numbers (such as separating into feet and inches).
Imagine that we have a variable "length" that we're trying to separate into "feet" and "inches".
(This is java code, but the same idea is true for C++).
feet = Math.signum(length) * Math.floor(Math.abs(length / 12));
// could also do feet = length>0 ? Math.floor(length / 12) : Math.ceil(length / 12)
inches = Math.abs(length) % 12;
If the length is between -1 feet and 0 feet, we'd want it to say -0 for the feet so we know it's negative.
Negative zero has for example some use when handling complex numbers...
In everyday use one should mostly avoid the negative zero.
Some links with information regarding background/uses/pitfalls of "negative zero":
http://en.wikipedia.org/wiki/Signed_zero
http://en.wikipedia.org/wiki/Floating_point#Signed_zero
http://en.wikipedia.org/wiki/Branch_cut
http://connect.microsoft.com/VisualStudio/feedback/details/344366/negative-zero-behavior-between-c-and-c-code-is-different
http://connect.microsoft.com/VisualStudio/feedback/details/292276/in-vs2005-c-zero-reported-as-negative-zero-for-double-type
C++ ceil and negative zero