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 3804352
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:30:17+00:00 2026-05-19T14:30:17+00:00

in c-programming, if I use the variables k, X, Y, n in the equation

  • 0

in c-programming, if I use the variables k, X, Y, n in the equation below (where exp() is math constant e raised to (), pi = 3.14159…, and j = sqrt(-1)) and these variables are all declared as 64b double precision floating-pt numbers (where X is a complex number), the result for output will also be this data type.

output = k * exp(2*pi*j*X*Y/n)

Now, if I want to try to manage memory more intelligently, I’d like to make

k, Y, and n to be 32bit unsigned long int (0 to 4294967295), and

pi, j, and X to be 32bit float.

My question is, will the result for output be data type float (hopefully) or unsigned long int? That is, does C language have some default rules that intelligently takes care of multiplying a float and integer and returning the result as float, or must I manage that manually using type casting, etc? Just wondering if there’s anything I need to worry about in such an operation, or if I can leave it to C to do everything intelligently behind the scenes.

  • 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-05-19T14:30:18+00:00Added an answer on May 19, 2026 at 2:30 pm

    This can get kind of ugly. The compiler looks at the types of the operands for a single operation, and promotes both to the “larger” type (e.g., if one is int and the other double, it’ll convert the int to double, then do the operation).

    In your case, that could have some rather unexpected results. Right now you have: 2*pi*j*X*Y/n. The operators group from left to right, so this is equivalent to ((((2*pi)*j)*X)*Y)/n. In this case, that’ll probably work out reasonably well — one of the operands in the “first” operation is a float, so all the other operands will be converted to float as you want. If, however, you rearrange the operands (even in a way that seems equivalent in normal math) the result could be completely different. Just for example, if you rearranged it to 2*Y/n*pi*j*X, the 2*Y/n part would be done using integer arithmetic because 2, Y, and n are all integers. This means the division would be done on integers, giving an integer result, and only after that integer result was obtained would that integer be converted to a float for multiplication by pi.

    Bottom line: unless you’re dealing with something like a large array so converting to smaller types is likely to really save quite a bit of memory, you’re generally much better off keeping all the operands of the same type if possible. I’d also note that in this case, your attempt at “managing memory intelligently” probably won’t do any good anyway — on a typical current machine, a long int and a float are both 32 bits, so they both use the same amount of memory in any case. Also note that exp takes a double as its operand, so even if you do float math for the rest, it’ll be promoted to a double anyway. Also note that conversions from int to float (and back) can be fairly slow.

    If you’re really only dealing with a half dozen variables or so, you’re almost certainly best off leaving them as double and being done with it. Converting to a combination of float and long will save about 14 bytes of data storage, but then add (around) 14 bytes of extra instructions to handle all the conversions between int, float, and double at the right times, so you’ll end up with slower code that uses just as much memory anyway.

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

Sidebar

Related Questions

What fonts do you use for programming, and for what language/IDE? I use Consolas
In what areas of programming would I use state machines ? Why ? How
I'm trying to use DSDP (semidefinite programming package) with cvxopt. I have both of
this is a question that when programming I always wonder: What to use when
In which situations should I choose to use a functional programming language over a
I'm thinking of asking my students to use git for pair programming. Because student
I've never had much need for programming with databases. Since their use is so
I'm a VMware user and far too often I use keyboard shortcuts while programming.
I am currently doing some socket programming using C/C++. To be able to use
I create business applications with heavy database use. Most of the programming work is

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.