Subscribe now and choose from over 30 free gifts worth up to £49 - Plus get £25 to spend in our shop
In simple terms I want to find out the order of magnitude for a given number.
e.g.
1,000 = 3
100,000 = 5
On Windows 7 the built-in calculator (in scientific mode) has a log function so log(1000) = 3.
Does anyone know the equivalent function in C#. Brain fried trying to remember A level Maths...
Math.Ceiling(Math.Log10(x))
no prize?
here you go ...
// Prize.cs
public class stwPrize
{
public static void Main()
{
System.Console.WriteLine("Torminalis, You Win!");
}
}
😀
Cheers Torm, that did the trick perfectly you win first prize!
you guys and your modern programming....
i remember writing that in pure C once
and also in .......................................
ASSEMBLER!
In future try http://www.stackoverflow.com - more geeks than you can shake a sweaty Megadeth t-shirt at.
uh, it doesn't work for negative numbers, and shouldn't it be Floor() ?
It won't as log of anything less than 0 is complex and fortran is the only language (I know of) that natively supports complex numbers.
Take the absolute value first, just about every language had intrinsic funciotn for htese basisc maths such abs, log, mod e.t.c
you can't compute the log of a (real) negative number...uh, it doesn't work for negative numbers, and shouldn't it be Floor() ?
EDIT:
TheBrick - MemberIt won't as log of anything less than 0 is complex and fortran is the only language (I know of) that natively supports complex numbers.
Take the absolute value first, just about every language had intrinsic funciotn for htese basisc maths such abs, log, mod e.t.c
Ah too slow! - Python natively supports complex numbers, but even then it wouldn't automatically return a complex result from the log(x) function.
here you go maths.abs
http://msdn.microsoft.com/en-us/library/system.math.abs(v=vs.71).aspx
you can't compute the log of a (real) negative number...
I know, that why it doesn't work for them...
and doesn't Ceiling() round up ?
if torm's perfect answer isn't good enough you convert it to a string then interrogate the length of it.
Ah too slow! - Python natively supports complex numbers, but even
then it wouldn't automatically return a complex result from the log(x) function.
Interesting. I wrote my first python script a few weeks ago to do some simple text parsing and thought it was quite a cool little language (in my limited experience). I think it's even better now.
if torm's perfect answer isn't good enough you convert it to a string then interrogate the length of it.
unless negative or containing decimals...
unless negative or containing decimals...
oh true, there's no way of overcoming those issues
oh true, there's no way of overcoming those issues
I didn't mean that - but programming has to be exact, not just winging it and hoping for the best 🙂
but programming has to be exact, not just winging it and hoping for the best
There was no specification of needing to deal with anything but unsigned integers, if you look at the OP he did in fact specify that he was looking for the equivalent of an existing function that he had already identified as doing the job and I merely pointed him to the equivalent function in C#.
I take it you work in research and not in the real world? 😉
i seem to remember a classic Picture of a Swing, and its stages through the development life cycle.
ahhh.....how things DONT change!
There was no specification of needing to deal with anything but unsigned integers
neither was there anything to exclude them. 2 examples is hardly a specifiucation. Surely the "real world" does throw up exactly this kind of unexpected eventuality ?
aaah, in two weeks I leave programming to start a role in functional design / system reqs.
I am so happy, I've always struggled with programming, been doing it professionally for 3 years now, yes I can write code that works, but I've never really enjoyed it...
Anyhow for the avoidance of any doubt, Torm's code is now embedded into the codebase of a future production release.
I only need to deal with postive values so it does what it says on the tin. I think I've spent too much time coding PHP and forgotten what C# looks like!
FFS the original poster is happy with the solution why do you need to try and pick holes in it. If it's fit for purpose then it's fine and I'm sure the OP will be validating the input before attempting to find the log of a negative, ok now?
If it's fit for purpose then it's fine
"if" being the appropriate word, and since when was the user responsible for robust program design ? Remember 2 digits for the year ?
"if" being the appropriate word, and since when was the user responsible for robust program design ? Remember 2 digits for the year ?
Do you really not have anything better to do?
and don't forget zero is a valid positive number yet its order of magnitude is embarrassingly -infinity...
Do you really not have anything better to do?
tell that to the millenium
If you have a better solution, show us.
If you have a better solution, show us.
do we know what the question is yet ?
Zero is great! It does all kind of freaky things.
do we know what the question is yet ?
On Windows 7 the built-in calculator (in scientific mode) has a log function so log(1000) = 3.Does anyone know the equivalent function in C#.
If you want to know the gory details it is to calculate the order of magnitude of a cost (so this will always be positive as in business there is no such thing as a negative cost and the system does not allow negative values via the data entry validation).
The plan is to figure out the order of magnitude of the maximum value of a data point plotted on the axis of a bar chart. And this needs to be a round figures to look nice. eg
max x value = 965 -> x-axis range = 1000
max x value = 6713 -> x-axis range = 6800
i.e. 965.
Math.Ceiling(Math.log10(965)) = 3
Minus 1 from [order of magnitude] (if greater than 1)
This would be 2.
Back again
Math.Pow(10, 2) = 100.
Calculate the max figure to the nearest 100 = 900
Add the interval to this figure = 900 + 100
Max x-axis value = 1000
Voila
But Crystal Report 2008 does not allow one to programmatically set the x-axis max range (you can only do this via the designer).
So its all been for nowt, ah well that's dev for you. Back to my cup of coffee and the next bug...
I wrote something very similar in Flash Action script. for a plotter. The max axis calculation leads to some strange axis values, which can lead to strange intervals, if you fix the number of intervals. Folks are much happier reading from charts with familiar maxima, e.g 100,200,500,1000 etc. Anything else tends to lead to misinterpretation of the data.
Check it out the OP knew the question after all, so no need for anyone else to ask if for him 🙂
Ah Crystal reports - best when in the dustbin!
Crystal Reports PITAS...
If you set the report to auto-range on a given axis and the report is say 5 pages long. The x-axis max range will be set to the max value for that page. So the max range for page 1 may be 8000, page 2 is 200, etc.
My idea was to set this programmatically and disable the auto-range property. My mistake was to assume that CR2008 supports setting the max-range value dynamically.
as in business there is no such thing as a negative cost
YAY! Trebles and bonusses all round ?
