W3Ask






HOME  >  PROGRAMMING  >  GLOBAL PROGRAMMING

Design a function f(f(n)) == -n

Where n is a 32 bit signed integer.

Complex numbers arithmetic are forbidden.

Design it for the largest range possible, knowing that you can’t design it for the whole range of numbers.

The solution is to use Math Ceiling :

static double F(double n){
if (n == 0) return 0;
if (n < 0) return ((long)Math.Ceiling(n) % 2 == 0) ? (n + 1) : (-1 * (n - 1));
else return ((long)Math.Floor(n) % 2 == 0) ? (n - 1) : (-1 * (n + 1));
}





Related posts:


Could not instantiate mail function on WordPress
https://w3ask.com/could-not-instantiate-mail-function-wordpress/
If you get the “Could not instantiate mail function” message when you try the contact page in WordPress, that means that you have an issue ...
Functioning of wind turbines
https://w3ask.com/functioning-wind-turbines/
The electric power delivered by a wind turbine depends on the length and shape of its blades, the temperature that will vary the density of ...
The best multifunction color laser printers
https://w3ask.com/best-multifunction-color-laser-printers/
Here is the list of the best multifunction printers with laser technology and which print in color. The reviews come from several online ...
Top of the multifunction color laser printers
https://w3ask.com/top-multifunction-color-laser-printers/
Here are the top two of the most efficient multifunction color laser printers and a brief description for both of them. Color laser printer ...
The best multifunction black and white laser printer
https://w3ask.com/best-multifunction-black-white-laser-printer/
List of the best laser printers with scanner that print in black and white. PPM : pages per minutes. Reviews: reviews from different online ...
Trading System Forex GadgetVersus GadgetVersus France