Hi, i'm looking for resources on how to do simple math in c# (asp.net core). I think this is an important topic that most developers come across at some point.

I'm making an application in asp.net core where I have to do some extensive calculations and I see now that i'm lacking in programming maths. For example; some types like ints 1) aren't good at using for division. 2) And you must also make sure dynamic variables
that are used in division is not 0! 3) types have limitations in range and types with decimals lose presicion at some point too! 4) memory allocations: float is the same as int, but double is twice the size and decimal is huge! doing lots of calculations with
collections with thousands of decimals might not be the best idea(?)! 5) casting is a delicate topic because sometimes you can't show decimals in the frontend and you must convert to ints or some readable value. 6) decimal type should be avvoided for anything
but money(and it is more advanced to use in my experience), int is for counting mostly, float is usually best for most use cases(?). i tend to convert from float to int but not sure if it's always a good idea.

things like that are stuff that i've had to learn the hard way, and i'm absolutely certain i have a lot to learn that are basic things like I mentioned. I'm scared i will make mistakes that will cost my employer!

So this topic is very important but for some reason i have never seen any developers talk that much about it on blogs or tutorials.

Do you have any tips on where i can find good resources for this? anything is of interest.

1) ints are fine for division if you want to do division of whole numbers. If you want to divide 13 people into 4 groups then you do not want an answer of 3 and a third.

2) Of course. Just like you would have learnt in primary school

3) Yes. When programming you should always choose types that are appropriate for your need. If you want to model enrolments in a course then do not use a floating point type. If you are doing money calculations then use decimal. If you are doing high
precision scientific work then use a high precision floating point type.

4) 8 bytes for a decimal (or is it 16 - I generally don't care) types thousands of variables equals a memory allocation of about 0.000001% of the memory of a typical small computer. Probably not something to worry about.

General advice -

As always choose a type which best models the item being represented. This is the most important thing.

Sanity check all inputs. If you get a request to divide some employees into zero teams then you cannot work out how many go in each team! Division by zero should not be an issue if you inputs have been checked for sanity.

Member

153 Points

185 Posts

## Math in c# for beginners and beyond?

May 23, 2019 08:15 AM|bluMarmalade|LINK

Hi, i'm looking for resources on how to do simple math in c# (asp.net core). I think this is an important topic that most developers come across at some point.

I'm making an application in asp.net core where I have to do some extensive calculations and I see now that i'm lacking in programming maths. For example; some types like ints 1) aren't good at using for division. 2) And you must also make sure dynamic variables that are used in division is not 0! 3) types have limitations in range and types with decimals lose presicion at some point too! 4) memory allocations: float is the same as int, but double is twice the size and decimal is huge! doing lots of calculations with collections with thousands of decimals might not be the best idea(?)! 5) casting is a delicate topic because sometimes you can't show decimals in the frontend and you must convert to ints or some readable value. 6) decimal type should be avvoided for anything but money(and it is more advanced to use in my experience), int is for counting mostly, float is usually best for most use cases(?). i tend to convert from float to int but not sure if it's always a good idea.

things like that are stuff that i've had to learn the hard way, and i'm absolutely certain i have a lot to learn that are basic things like I mentioned. I'm scared i will make mistakes that will cost my employer!

So this topic is very important but for some reason i have never seen any developers talk that much about it on blogs or tutorials.

Do you have any tips on where i can find good resources for this? anything is of interest.

thanks for any answers!

Participant

1020 Points

541 Posts

## Re: Math in c# for beginners and beyond?

May 23, 2019 08:29 AM|PaulTheSmith|LINK

1) ints are fine for division if you want to do division of whole numbers. If you want to divide 13 people into 4 groups then you do

notwant an answer of 3 and a third.2) Of course. Just like you would have learnt in primary school

3) Yes. When programming you should always choose types that are appropriate for your need. If you want to model enrolments in a course then do not use a floating point type. If you are doing money calculations then use decimal. If you are doing high precision scientific work then use a high precision floating point type.

4) 8 bytes for a decimal (or is it 16 - I generally don't care) types thousands of variables equals a memory allocation of about 0.000001% of the memory of a typical small computer. Probably not something to worry about.

General advice -

As always choose a type which best models the item being represented. This is the most important thing.

Sanity check all inputs. If you get a request to divide some employees into zero teams then you cannot work out how many go in each team! Division by zero should not be an issue if you inputs have been checked for sanity.

Memory allocation is a performance issue. You do not have a performance issue until you have working code and you actually experience a problem. Premature optimization is the root of all evil.