-1

So I have some numerical code which writes a time step as the file name. I need to use quite small time steps and for some reason I can't get the code to write the float value to my file name. Here's a MWE

#include <iostream>
#include <fstream>
#include <random>
#include <chrono>
#include "nsoliton.hpp"

int main()
{

float step = 2/10; // MAKE A DECIMAL
int tmax = 100.;

std::cout.precision(4);
std::cout << "step =" << std::setw(step) << std::endl;

std::fstream file3(std::to_string(step) + "_t1.txt", std::ios::out);


    return 0;
}

// g++ -c -O2 -std=c++11 -fopenmp floatMWE.cpp; g++ -fopenmp -O2 -o floatMWE floatMWE.o -lmpfr -lgmp; ./floatMWE

The goal should be to have sequential files labelled e.g.,

0_t1.txt, 1_t1.txt ...

but obviously accounting for the step so non-integer

any clues?

I tried using ``setw'' but that still doesn't work. Checked also using a print command and it too won't print the float value. I also tried changing `'step'' from a float to an int but still nothing.

  • `float step = 2/10; // MAKE A DECIMAL` this will initialize `step` to `0`, because of integer division. You want `float step = 2.0f/10; // MAKE A DECIMAL` to make it a floating point operation. – mch Aug 10 '23 at 07:45
  • 1
    2/10 gives zero. Yes, really. – lastchance Aug 10 '23 at 07:45
  • It is a pity that you didn't show the printed output nor the file names. This contains valuable information. "doesn't work" and "nothing" are uninformative and wrong. – Yves Daoust Aug 10 '23 at 07:46
  • You can enable a compiler option that parses the comments so `//make a decimal` coerces the compiler to do so. /s – infinitezero Aug 10 '23 at 07:48
  • @YvesDaoust Good morning to you too, you could ask nicely. "doesn't work" means no file output and "nothing" is.. well.. nothing. No output... – GregoryHouse Aug 10 '23 at 07:49
  • `float step = 2/10; ` replace with `float step = 2.0f/10.0f; ` to get a floating point division. Note that this will be an approximation of 0.2 (decimal), since float cannot represent that value exactly right. – Pepijn Kramer Aug 10 '23 at 08:01
  • I want you to understand that you need to give us enough feedback. Which you still didn't. No output is not possible, `cout` does work. – Yves Daoust Aug 10 '23 at 08:02
  • 1
    Even floating point division is not going to produce the output the OP requests. It seems that they want to put the number of steps not the current value of `step` into the file name. But it's not very clear. – john Aug 10 '23 at 08:58
  • It sounds like you tried everything *except* examining what the value of `2/10` is. – molbdnilo Aug 10 '23 at 09:35
  • @YvesDaoust what do you want from me at this point? do I need to spell it out on an atomic level? Cout does indeed work but it doesn't print the value I was looking for. (no thanks to you) I solved it anyway... Maybe be more constructive in how you address beginners to C++ – GregoryHouse Aug 10 '23 at 10:25
  • "Cout does indeed work but it doesn't print the value I was looking for" is a very different statement from "No output". You want to be accurate. For future questions, typically what's useful for stuff like this is to include 1.) What output you expected and 2.) What output you actually got. You never included either. – Nathan Pierson Aug 10 '23 at 14:48
  • @NathanPierson no worries, I won't ask for help from this community again – GregoryHouse Aug 11 '23 at 10:13

0 Answers0