Arithmetic rules of data type in language c

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

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

Now, if I want to manage memory more intelligently, I would like to do

k, Y and n are 32-bit unsigned long int (from 0 to 4294967295) and

pi, j, and X is a 32-bit float.

My question is: will the result be a float data type (hopefully) or an unsigned long int? That is, in the C language, there are some default rules that reasonably take care of multiplying the float and integer and return the result as a float, or should I manage it manually using type casting, etc.? Just wondering if there is anything I need to worry about in such an operation, or if I can leave it in C to do everything wisely behind the scenes.

+3
source share
3 answers

It may look ugly. The compiler looks at the types of operands for one operation and promotes both types of "larger" ones (for example, if one intand the other double), it converts intto double, then perform the operation).

. : 2*pi*j*X*Y/n. , ((((2*pi)*j)*X)*Y)/n. , , - "" - float, float . , , ( , ), . , 2*Y/n*pi*j*X, 2*Y/n , 2, Y n . , , , , , float pi.

: - , , , , , , , , , " ", -, - , long int a float 32 , . , exp double , , float , double. , int float ( ) .

, double . float long 14 , () 14 int, float double , , .

+5

float ints float. :

  • float * float = float
  • float * int = float
  • int * float = float
  • INT * INT = Int

" ", int.

+8

" " C : float:

  • , .
  • If one of the two, the other is converted to double.
  • Otherwise, if someone swims, the other will be converted to float.
  • If they are both integers, different "whole promotions" rules apply.

The numeric constant "1" is considered int. The numeric constant "1.0" is considered double. The numeric constant "1.0f" is considered floating.

+2
source

Source: https://habr.com/ru/post/1787864/


All Articles