r/dailyprogrammer • • Oct 27 '12
[10/27/2012] Challenge #108 [Easy] (Scientific Notation Translator)
If you haven't gathered from the title, the challenge here is to go from decimal notation -> scientific notation. For those that don't know, scientific notation allows for a decimal less than ten, greater than zero, and a power of ten to be multiplied.
For example: 239487 would be 2.39487 x 105
And .654 would be 6.54 x 10-1
Bonus Points:
- Have you program randomly generate the number that you will translate.
- Go both ways (i.e., given 0.935 x 103, output 935.)
Good luck, and have fun!
26
Upvotes
4
u/andkerosine Oct 27 '12
Just for reference, 6.54 × 10-1 would be the "standard" way to write it.
14
u/[deleted] Oct 27 '12
Why are you all using loops? It's unnecessary.
4
u/[deleted] Oct 27 '12 edited Oct 27 '12
A little verbose, as usual.
Java with scientific to decimal bonus:
2
u/[deleted] Oct 27 '12
I noticed this accidentally prints... well, interesting results for complex numbers.
And sure enough: http://www.wolframalpha.com/input/?i=%281.66382%2B1.85251i%29+*+10%5Ei
This behaviour is too cute for me to fix.
2
u/[deleted] Oct 30 '12
Java. Excuse my naming conventions. Tips are appreciated
2
u/[deleted] Oct 31 '12 edited Oct 31 '12
Very new to programming and just found this sub, here is my shot with VB
2
u/Doggabyte Oct 31 '12
c#
3
u/the_mighty_skeetadon Oct 28 '12 edited Oct 28 '12
Late to the game, but decided to solve this without using any math at all =). Literally zero math, unless you count regex or string comparisons as math (I don't). In Ruby:
Because math is icky. Well, not really, but this seemed like more fun. FYI, Ruby's handling of floats is terrible, so I could re-implement this as something that handles strings instead, but I'm too lazy =P. Just don't try to do any tiny-tiny floats, as Ruby puts them in scientific notation without asking you, and this doesn't work with that =P.
1
u/prondose 0 0 Oct 27 '12
Perl:
Sample output
1
u/swarage 0 0 Oct 29 '12
my Ruby code is so verbose that it has to be in a pastebin link : http://pastebin.com/dyReMwxd
1
u/[deleted] Oct 29 '12
C++:
1
u/cdelahousse Oct 30 '12
Javascript. Rewrote it a few time.
1
u/rowenlemming Oct 30 '12 edited Oct 30 '12
Here's a quick solve in Javascript
1
u/spectrum86 Nov 03 '12
My programming class uses Ada for everything so that's what I wrote this in. It feels pretty wordy to me but so does most of Ada. By default the language outputs floats in <number>E<exponent> format. so I intentionally sidestepped that functionality when writing this. Any feedback would be appreciated.
1
u/[deleted] Nov 04 '12
Java
1
u/[deleted] Nov 05 '12
SML (Mosml)
As quite messy solution.
1
u/[deleted] Nov 08 '12 edited Nov 08 '12
Python:
EDIT: Did the optional part and removed some boilerplate code.
1
u/ahlk 0 0 Nov 24 '12 edited Nov 24 '12
Not sure if printf is not allowed, or people didn't realize there's an escape for this. Works both ways
Perl
output:
1
u/no1warlord Jan 15 '13
VB.NET (noob lang):
1
u/EvanMaker Oct 27 '12
In Ruby, am i doing it right? ( Gets input and transform it to Scientific Notation )
1
u/Rapptz 0 0 Oct 27 '12
Output:
1
u/atticusalien 0 0 Oct 27 '12
C# Random Double -> Scientific -> To Double
1
u/fluffy_cat Oct 27 '12
I went down the user input route, with inelegant fixes for floating point weirdness.
Sample inputs / outputs
Doesn't work with scientific notation input using negative exponents, because I am rubbish.
1
u/nagasgura 0 0 Oct 27 '12 edited Oct 30 '12
Python:
Usage:
0
u/InvisibleUp Oct 27 '12
This feature is built into printf. Seems easy enough. [C]
-1
u/skeeto -9 8 Oct 27 '12
In Emacs Lisp,
Example output,