Last active
December 10, 2015 21:18
-
-
Save ghalimi/4494374 to your computer and use it in GitHub Desktop.
COMPLEX Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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