CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2012
    Posts
    127

    Mesh Array of double boost

    I wanna create mesh Array using boost elements, that will be do the same that code below, but can accept numbers of double.
    Please advice me any boost array that give me needed functionality. It will be great that advised element has the same argument list. I mean
    // [base,stride,bound)
    // [0,2,4)
    P.S. Or may be there are any opportunity modify this boost::multi_array_types::index_range in order do make it accept double...
    Many thanks in advance!

    1>------ Build started: Project: HP_A.1_d_, Configuration: Debug Win32 ------
    1> Main.cpp
    1>c:\all my\с++\ha level 9\solution\level 9\hp_a.1_d_\main.cpp(15): warning C4244: 'argument' : conversion from 'double' to '__w64 int', possible loss of data
    1>c:\all my\с++\ha level 9\solution\level 9\hp_a.1_d_\main.cpp(15): warning C4244: 'argument' : conversion from 'double' to '__w64 int', possible loss of data
    1>c:\all my\с++\ha level 9\solution\level 9\hp_a.1_d_\main.cpp(18): warning C4244: 'argument' : conversion from 'double' to 'unsigned int', possible loss of data
    1>c:\all my\с++\ha level 9\solution\level 9\hp_a.1_d_\main.cpp(20): warning C4244: 'argument' : conversion from 'double' to 'unsigned int', possible loss of data
    1>c:\all my\с++\ha level 9\solution\level 9\hp_a.1_d_\main.cpp(20): warning C4244: 'argument' : conversion from 'double' to 'unsigned int', possible loss of data
    1> HP_A.1_d_.vcxproj -> C:\all my\с++\HA level 9\Solution\Level 9\Debug\HP_A.1_d_.exe
    ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========


    Code:
    #include <boost\multi_array.hpp>
    #include <iostream>
    typedef boost::multi_array_types::index_range range;
    
    using namespace std;
    
    int main()
    { 
    	// [base,stride,bound)
      // [0,2,4) 
    
      
      range a_range;
      a_range = range(0,23.2,3.4);
      double i=0;
      
      while (a_range[i] <=a_range.get_finish())
    	 {
      cout  <<"for value: " <<  a_range[i]<< " => " << a_range[i]*4.5 << "\n";
      ++i;
    	 }
    
      return 0;
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Mesh Array of double boost

    Quote Originally Posted by oteel View Post
    I wanna create mesh Array using boost elements, that will be do the same that code below, but can accept numbers of double.
    First, doubles are inexact to begin with. So why are you attempting to create ranges (and manipulate them) using inexact types?

    The solution is to use integer. Instead of using doubles, just multiply those values by a factor of 10.
    Code:
    range a_range;
    a_range = range(0,232,34);
    unsigned int i=0;
    
    while (a_range[i] <=a_range.get_finish())
    {
        cout  <<"for value: " <<  a_range[i]/10.0<< " => " << a_range[i]/10.0*4.5 << "\n";
        ++i;
    }
    Note how the range is all integers, and inside the loop, the number is adjusted for the calculations.

    You never should use doubles as loop counters or anything that requires exact counting. Your original code where you had doubles could possibly run differently depending on compiler, compiler options, etc.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured