locked
scientific notation RRS feed

  • Question

  • Hi all,

     I have tried this to force scientific notation, on the screen it looks good.

     But when I debug some does and others do not. is there a solution to this?

    #include <iostream>
    #include <iomanip>
    #include <sstream>
    #include <cmath>
    
    int main()
    {
    	float var1= 0.00073063833f;
    	std::cout 
    		<< std::setprecision(4)
    		<< std::scientific << var1 << "\n";
    
    	float var2;
    	std::stringstream os;
    	os << std::scientific << std::setprecision(4) << var1;
    	os >> var2;
    	std::cout << var2;
    
    	std::cin.ignore();
    	std::cin.get();
    	return 0;
    }

    ollp

    Saturday, April 29, 2017 10:04 PM

Answers

  • Hi Wyck

    thank you so mutch.

    now its answered

    ollp

    • Marked as answer by ollp Tuesday, May 2, 2017 1:23 PM
    Tuesday, May 2, 2017 1:23 PM

All replies

  • Define "looks good"?

    -- pa


    • Edited by Pavel A Saturday, April 29, 2017 11:28 PM
    Saturday, April 29, 2017 11:28 PM
  • hi Pavel.

    yes I will saw it to late.

    it shows scientific notation 7.3064e-04. and that what I want.

    Both var1 and var2 but debugging they are std notation 0.00073063833

    ollp

    Sunday, April 30, 2017 9:26 AM
  • hi Pavel.

    yes I will saw it to late.

    it shows scientific notation 7.3064e-04. and that what I want.

    Both var1 and var2 but debugging they are std notation 0.00073063833

    ollp

    Why does it matter how the variables look in the debugger?

    If you want to see greater precision in the debugger, use double rather than float. There is no reason to use floats unless you lave large arrays and storage constraints.


    David Wilkinson | Visual C++ MVP

    Sunday, April 30, 2017 9:44 AM
  • hi Pavel.

    yes I will saw it to late.

    it shows scientific notation 7.3064e-04. and that what I want.

    Both var1 and var2 but debugging they are std notation 0.00073063833

    ollp

    Why does it matter how the variables look in the debugger?

    If you want to see greater precision in the debugger, use double rather than float. There is no reason to use floats unless you lave large arrays and storage constraints.


    David Wilkinson | Visual C++ MVP

    Hi David

    yes it does a little complicated math. else I have to use calculater a lot.

    double or float same result.

    I do get NaN a few times and need to know were it goes wrong.

    ollp


    • Edited by ollp Sunday, April 30, 2017 10:13 AM forgot about the double
    Sunday, April 30, 2017 9:57 AM
  • it shows scientific notation 7.3064e-04. and that what I want.

    Both var1 and var2 but debugging they are std notation 0.00073063833

    As far as I know it is not possible to customize the debugger's display of primitive types (e.g., float).
    Sunday, April 30, 2017 10:44 AM
  • Why does it matter how the variables look in the debugger?

    If you want to see greater precision in the debugger, use double rather than float. There is no reason to use floats unless you lave large arrays and storage constraints.


    David Wilkinson | Visual C++ MVP

    Hi David

    yes it does a little complicated math. else I have to use calculater a lot.

    double or float same result.

    I do get NaN a few times and need to know were it goes wrong.

    ollp

    What do you mean 'double or float same result'? I thought we were talking about display in the debugger -- surely doubles show more digits in the debugger than floats.

    If you are getting Nan's you need to step through the code in the debugger.


    David Wilkinson | Visual C++ MVP

    Sunday, April 30, 2017 1:36 PM
  • Why does it matter how the variables look in the debugger?

    If you want to see greater precision in the debugger, use double rather than float. There is no reason to use floats unless you lave large arrays and storage constraints.


    David Wilkinson | Visual C++ MVP

    Hi David

    yes it does a little complicated math. else I have to use calculater a lot.

    double or float same result.

    I do get NaN a few times and need to know were it goes wrong.

    ollp

    What do you mean 'double or float same result'? I thought we were talking about display in the debugger -- surely doubles show more digits in the debugger than floats.

    If you are getting Nan's you need to step through the code in the debugger.


    David Wilkinson | Visual C++ MVP

    Hi David

    I mean it makes no difference if I use double or float, as you suggested I tried use double to get

    better precision. the result is the same just more decimals. Yes exactly, I use debugger to find the

    fault but as I said many numbers (meaning decimals) and hard to find what goes wrong. So shorten

    with scientific notation makes its more easy to read and find the problem. But seems not possible.

    I would have posted if the code was not so big and complicated. but it is

    ollp

    Sunday, April 30, 2017 2:21 PM
  • I mean it makes no difference if I use double or float, as you suggested I tried use double to get
    better precision. the result is the same just more decimals. Yes exactly, I use debugger to find the
    fault but as I said many numbers (meaning decimals) and hard to find what goes wrong. So shorten
    with scientific notation makes its more easy to read and find the problem.

    I really do not understand why it is so important to you how the values look in the debugger.


    David Wilkinson | Visual C++ MVP

    Sunday, April 30, 2017 6:28 PM
  • hi David.

    Its because some are displayed in scientific notation and some are not.

    And that makes it harder to follow when combined in variables, that shall be calculated to a final value.

    As and sample (-6.6370734E-07) that is not correct the correct value is (-6.7370734E-07)

    and o find that is difficult when there is a mixture displayed.

    ollp

    Sunday, April 30, 2017 7:12 PM
  • hi David.

    Its because some are displayed in scientific notation and some are not.

    And that makes it harder to follow when combined in variables, that shall be calculated to a final value.

    As and sample (-6.6370734E-07) that is not correct the correct value is (-6.7370734E-07)

    and o find that is difficult when there is a mixture displayed.

    ollp

    I thought you were just looking for the first appearance of Nan's...

    That aside, the error you describe is rather odd, with only the second-most significant digit incorrect.


    David Wilkinson | Visual C++ MVP

    Sunday, April 30, 2017 10:19 PM
  • When inspecting variables in the debugger, such as in the watch window, you can enter a format specifier that controls the formatting of the variable's value as text.

    Here's a reference for Format Specifiers in C++.

    For example:

    • ,e  float formats as "exponent" (sci notation)
    • ,f  formats as "float"  (beware of precision when using this notation with very large numbers)
    • ,g  formats as "general" (shorter of e and f)



    • Edited by Wyck Tuesday, May 2, 2017 1:49 AM
    • Proposed as answer by Baron Bi Tuesday, May 2, 2017 9:52 AM
    Tuesday, May 2, 2017 1:48 AM
  • Hi Wyck

    thank you so mutch.

    now its answered

    ollp

    • Marked as answer by ollp Tuesday, May 2, 2017 1:23 PM
    Tuesday, May 2, 2017 1:23 PM