-1

I'm doing a proof of concept that involves porting some Pascal code over to C#.
The code involves financial calculations, so most of the fields are currency based (though not all).

All of the numeric fields are defined as Real data type in Pascal. Because the code deals with mainly currency fields, I had decided to use the decimal data type in C#.

I have come across a piece of code that seems to do a "power of" in Pascal (it is a double asterisk). It does not round the results of the power call straight away (it may do later I'm not 100% sure as there are thousands of lines of code).

Out of the box in C# the Math.Pow method only accepts double data types.
I'm wondering as an overall concept/rule of thumb - should I be using double instead of decimal to begin with if trying to port Pascal Real data types over to C#?
Or what would people recommend?

14
  • 3
    A pascal Real is either 32 or 64 bit floating point value depending on the system. So if you want to have the closest C# implementation, pick either float or double accordingly. You can use Math.Pow with both. Commented May 12 at 11:20
  • 2
    FYI double and float are not suitable for financial calculations as they have some inaccuracy which can compound over multiple operations. See What is the best data type to use for money in C#? Commented May 12 at 11:32
  • 1
    @MindSwipe you are right in general. But when porting a large code base, preserving the behavior (i.e. stabilty) is a consideration. You can always change to decimal in a second, later stage after you verified the code behaves as it did. Commented May 12 at 11:34
  • 2
    Regarding the dupes: I think a main issue here is the porting from pascal, which is not addressed at all in the dupes. Voted to reopen. Commented May 12 at 12:11
  • 1
    @wohlstad To be honest, this question should really be closed as Needs Details. We have no code, so no idea what it's really meant to be doing. Also, the question doesn't seem to be about how to port Pascal code, OP seems to already have done that, the question seems to be about how to do Math.Pow on decimal, so I thought I'd be nce and point to a duplicate. Commented May 12 at 12:11

1 Answer 1

0

should I be using double instead of decimal to begin with if trying to port Pascal Real data types over to C#?

C#'s decimal might be indeed better suited for currency values, if you wrote code from scratch.

But Real in pascal is actually a 32 or 64 bit floating point value on most common platforms (depending on the system), usually based on IEEE 754 standard.
So if you want to have the closest C# implementation (at least for the first stage of porting) on such systems - you should pick either float (for 32 bit) or double (for 64 bit).

You can use Math.Pow with either float or double.

This assumes that stability is am important consideration for the porting project.
After you verified your ported code behaves as expected, you can refactor it to use decimal.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.