C# Maths function (...
 

Subscribe now and choose from over 30 free gifts worth up to £49 - Plus get £25 to spend in our shop

[Closed] C# Maths function (geek programming content)

35 Posts
11 Users
0 Reactions
349 Views
Posts: 0
Topic starter
 

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...


 
Posted : 09/02/2011 5:07 pm
Posts: 0
Free Member
 

Math.Ceiling(Math.Log10(x))


 
Posted : 09/02/2011 5:09 pm
Posts: 0
Free Member
 

no prize?


 
Posted : 09/02/2011 5:21 pm
Posts: 8
Free Member
 

here you go ...

// Prize.cs
public class stwPrize
{
public static void Main()
{
System.Console.WriteLine("Torminalis, You Win!");
}
}


 
Posted : 09/02/2011 5:24 pm
Posts: 0
Free Member
 

😀


 
Posted : 09/02/2011 5:25 pm
Posts: 0
Topic starter
 

Cheers Torm, that did the trick perfectly you win first prize!


 
Posted : 09/02/2011 5:26 pm
Posts: 0
Free Member
 

you guys and your modern programming....

i remember writing that in pure C once
and also in .......................................
ASSEMBLER!


 
Posted : 09/02/2011 7:24 pm
Posts: 31206
Full Member
 

In future try http://www.stackoverflow.com - more geeks than you can shake a sweaty Megadeth t-shirt at.


 
Posted : 09/02/2011 8:10 pm
Posts: 0
Free Member
 

uh, it doesn't work for negative numbers, and shouldn't it be Floor() ?


 
Posted : 09/02/2011 8:28 pm
Posts: 4954
Free Member
 

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


 
Posted : 09/02/2011 8:34 pm
 poly
Posts: 8747
Free Member
 

uh, it doesn't work for negative numbers, and shouldn't it be Floor() ?
you can't compute the log of a (real) negative number...

EDIT:

TheBrick - Member

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


Ah too slow! - Python natively supports complex numbers, but even then it wouldn't automatically return a complex result from the log(x) function.


 
Posted : 09/02/2011 8:36 pm
Posts: 4954
Free Member
 

here you go maths.abs
http://msdn.microsoft.com/en-us/library/system.math.abs(v=vs.71).aspx


 
Posted : 09/02/2011 8:36 pm
Posts: 0
Free Member
 

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 ?


 
Posted : 09/02/2011 8:59 pm
Posts: 0
Free Member
 

if torm's perfect answer isn't good enough you convert it to a string then interrogate the length of it.


 
Posted : 09/02/2011 9:12 pm
Posts: 4954
Free Member
 

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.


 
Posted : 09/02/2011 9:13 pm
Posts: 0
Free Member
 

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...


 
Posted : 09/02/2011 10:06 pm
Posts: 0
Free Member
 

unless negative or containing decimals...

oh true, there's no way of overcoming those issues


 
Posted : 09/02/2011 10:09 pm
Posts: 0
Free Member
 

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 🙂


 
Posted : 09/02/2011 10:15 pm
Posts: 0
Free Member
 

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? 😉


 
Posted : 10/02/2011 9:27 am
Posts: 0
Free Member
 

i seem to remember a classic Picture of a Swing, and its stages through the development life cycle.

ahhh.....how things DONT change!


 
Posted : 10/02/2011 9:38 am
Posts: 0
Free Member
 

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 ?


 
Posted : 10/02/2011 9:43 am
Posts: 5938
Free Member
 

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...


 
Posted : 10/02/2011 9:47 am
Posts: 0
Topic starter
 

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!


 
Posted : 10/02/2011 9:53 am
Posts: 5753
Full Member
 

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?


 
Posted : 10/02/2011 11:07 am
Posts: 0
Free Member
 

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 ?


 
Posted : 10/02/2011 12:27 pm
Posts: 0
Free Member
 

"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?


 
Posted : 10/02/2011 12:52 pm
Posts: 0
Free Member
 

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


 
Posted : 10/02/2011 1:07 pm
Posts: 0
Free Member
 

If you have a better solution, show us.


 
Posted : 10/02/2011 1:12 pm
Posts: 0
Free Member
 

If you have a better solution, show us.

do we know what the question is yet ?


 
Posted : 10/02/2011 1:41 pm
Posts: 0
Free Member
 

Zero is great! It does all kind of freaky things.


 
Posted : 10/02/2011 4:16 pm
Posts: 0
Free Member
 

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#.


 
Posted : 10/02/2011 4:18 pm
Posts: 0
Topic starter
 

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...


 
Posted : 10/02/2011 4:33 pm
Posts: 0
Free Member
 

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.


 
Posted : 10/02/2011 4:42 pm
Posts: 5753
Full Member
 

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!


 
Posted : 10/02/2011 4:43 pm
Posts: 0
Topic starter
 

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.


 
Posted : 10/02/2011 4:52 pm
Posts: 0
Free Member
 

as in business there is no such thing as a negative cost

YAY! Trebles and bonusses all round ?


 
Posted : 10/02/2011 5:44 pm