1

I use omnetpp-4.6 and veins-4a2 to execute my simulation.

I run this code which the variable "ch" corresponds to an id packet. In this example, 46.100 is the packet number 100 for the node 46 in my simulation.

I need to convert it to a float number, I used this method but it gives as result after converting it 46.1 and not 46.100. In my work 46.1 and 46.100 are two different packets.

#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
  std::string ch = "46.100";

  std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
  std::cout.precision(3);

  float var = atof(ch.c_str());
  EV <<"The value of var is: " <<  var << "\n";

}

I displayed all messages in my log file with EV. So, when I use std::cout it displays 46.100 but when I use EV it displays 46.1.

There is a method to convert the float number and have the correct number that I would using EV please?

Joe
  • 85
  • 1
  • 9
  • 2
    Possible duplicate of [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Richard Critten Apr 16 '17 at 19:03
  • 1
    1st don't use floating point for this; 2nd think about it mathematically `46.1 == 46.100` – Richard Critten Apr 16 '17 at 19:04
  • 1
    Sorry, I'm missing something - what's `EV`? – Qix - MONICA WAS MISTREATED Apr 16 '17 at 19:13
  • Also, @RichardCritten - your link and your second comment are irrelevant to the OP's question. I'm pretty sure OP understands why it's 46.1 instead of 46.100 - they're asking how to set fixed point float formatting in another stream. – Qix - MONICA WAS MISTREATED Apr 16 '17 at 19:14
  • EV can be used instead of ev to make logging more efficient in omnet++, you can see this link: https://omnetpp.org/doc/omnetpp4/api/group__Envir.html – Joe Apr 16 '17 at 19:20
  • Well you don't include any Omnet++ header file, so you haven't really created a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Apr 16 '17 at 19:23
  • If you need node 46, packet 100 to be different from node 46, packet 1, then `46.100` is fine for the first one but the second needs to be `46.001`. If you never have more than 999 packets on a node. If you can have more, use additional leading zeros such as `46.00100` and `46.00001` – Ben Voigt Apr 16 '17 at 23:49
  • Other comments have addressed why using a single variable or a floating point variable for this is a bad idea. Addressing only the question of how to reconfigure the formatting of EV: this is possible using an `#include ` and calling `EV << std::fixed << std::setprecision(3);` before outputting floating point values. – Christoph Sommer Apr 18 '17 at 06:53

0 Answers0