C++Guns – RoboBlog

06.01.2015

Fortran C++ Java

Filed under: — Thomas @ 10:01

Functions
Fortran:
C++

Class

Conversion
Asign a double to a float should trigger a warning. Due to the reduced precision of float. Asign a integer to a float should also warn. There exist big integers which cannot stored in float without loose of the last digits.
There are 4 different cases:
1) integer constant to float
2) integer variable to float
3) double constant to float
4) double variable to float

Fortran:

    integer :: i
    real(4) :: a
    real(8) :: b

    a = 1
    a = 100000001a
    a = i
    a = 0.1234567890123_8
    a = b

C++

  int i;
  float a;
  double b;

  a = 1;
  a = 100000001;
  a = i;
  a = 0.1234567890123;
  a = b;
gfortran  fortran.F90 -Wconversion -Wconversion-extra
fortran.F90:20.8:

    a = 1
        1
Warning: Conversion from INTEGER(4) to REAL(4) at (1)
fortran.F90:21.8:

    a = 100000001
        1
Warning: Conversion from INTEGER(4) to REAL(4) at (1)
fortran.F90:22.8:

    a = i
        1
Warning: Conversion from INTEGER(4) to REAL(4) at (1)
fortran.F90:23.8:

    a = 0.1234567890123_8
        1
Warning: Change of value in conversion from  REAL(8) to REAL(4) at (1)
fortran.F90:24.8:

    a = b
        1
Warning: Conversion from REAL(8) to REAL(4) at (1)


Initializer lists
Fortran:

Module TestModule
  contains
  subroutine func(list)
    implicit none
    integer, intent(in) :: list(:)

    write(*,*) list
  end subroutine
end module

program testprogrm
  use TestModule
  implicit none

  call func( (/1,2,3/) ) 
end program
$ gfortran -Wall -Wextra -fcheck=all fortran.F90
$ ./a.out 
           1           2           3

C++

#include < iostream >
#include < initializer_list >

void func(std::initializer_list list) {
  for(int x: list) std::cout << x << ' ';
}

int main() {
  func( {1,2,3} );
}
$ g++ -Wall -Wextra -std=c++11 cpp.cpp
$ ./a.out 
1 2 3

(Strongly typed) enumerations

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress