Skip to content

Instantly share code, notes, and snippets.

@ghalimi
Last active December 10, 2015 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghalimi/4494374 to your computer and use it in GitHub Desktop.
Save ghalimi/4494374 to your computer and use it in GitHub Desktop.
COMPLEX Function
// Copyright (c) 2012 Sutoiku, Inc. (MIT License)
function COMPLEX(real, imaginary, suffix) {
// Return error if either number is a non-numeric value
if (isNaN(real) || isNaN(imaginary)) return '#VALUE!';
// Set suffix
var suffix = (typeof suffix === 'undefined') ? 'i' : suffix;
// Return error if suffix is neither "i" nor "j"
if (suffix !== 'i' && suffix !== 'j') return '#VALUE!';
// Return complex number
if (real === 0 && imaginary === 0) {
return 0;
} else if (real === 0) {
return (imaginary === 1) ? suffix : imaginary.toString() + suffix;
} else if (imaginary === 0) {
return real.toString();
} else {
var sign = (imaginary > 0) ? '+' : '';
return real.toString() + sign + ((imaginary === 1) ? suffix : imaginary.toString() + suffix);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment