Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7827323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:49:00+00:00 2026-06-02T09:49:00+00:00

The following code is very repetitive: public static double Interpolate(double x1, double y1, double

  • 0

The following code is very repetitive:

 public static double Interpolate(double x1, double y1, double x2, double y2, double x)
    {
        return y1 + (x - x1) * (y2 - y1) / (x2 - x1);
    }
    public static decimal Interpolate(decimal x1, decimal y1, decimal x2, decimal y2, decimal x)
    {
        return y1 + (x - x1) * (y2 - y1) / (x2 - x1);
    }

However, my attempt to use generics does not compile:

 public static T Interpolate<T>(T x1, T y1, T x2, T y2, T x)
    {
        return y1 + (x - x1) * (y2 - y1) / (x2 - x1);
    }

The error message is as follows:

Error 2 Operator ‘-‘ cannot be applied to operands of type ‘T’ and ‘T’ C:\Git…\LinearInterpolator.cs

How should I reuse my code?

Edit: fast runtime is important for this modules.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T09:49:01+00:00Added an answer on June 2, 2026 at 9:49 am

    You can’t use operators in generics without specifying a base class constraint on the T.

    You might do something like this: Creating a Math library using Generics in C# but personally, unless you really have lots of different formulae then I don’t see much point.

    There’s also the possibility of dynamically compiling expression trees for T, rewriting the types between double/decimal from a ‘model’ tree generated by the C# compiler (keeping the arithmetic precedence etc)… if you’re really interested in that I can post the code for such a solution (I’ll need an hour or so though!). Runtime performance will be slower.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Give the following (straight-forward) code: public class pr1 { public static void f1(){ long
I have the following code that does something very simple: visits recursively a tree
the following code is very simple, but for some reason, for-loop does not iterate
The following code is a very simple ajax call to server that alerts back
The following JavaScript code is very confusing to me. Could anyone help me understand.
The following code to converts an IP to an int in a very fast
I have the following PHP code doing a very simple select into a table.
I have the following code in specman: var x := some.very.long.path.to.a.variable.in.another.struct; while (x ==
I have the following line of code called very often: var configValue = System.Configuration.ConfigurationManager.AppSettings[ConfigValueKey];
I used following code to get response from JSON response. It working very nicely

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.