Nikhil Nair

Greenhorn

Posts: 2

posted 3 years ago

I have written this small code to print square root as well as quadratic root.Is their a way to make numbers of digits after decimal to be printed dynamically(i.e according to the input sometimes 16 digits sometimes 15 digits after decimal) or is their any other way tweak the number of digits after decimal.

Thanks

I have written this small code to print square root as well as quadratic root.Is their a way to make numbers of digits after decimal to be printed dynamically(i.e according to the input sometimes 16 digits sometimes 15 digits after decimal) or is their any other way tweak the number of digits after decimal.

Thanks

Campbell Ritchie

Marshal

Posts: 56533

172

posted 3 years ago

Welcome to the Ranch

I have added code tags to your post; you should always use them. See how much better it looks Never import java.lang.Math.

You have misunderstood the figures in the format String. 1. does not mean one digit then the decimal point. It means take 1 space for the whole output. You want 16 for the places after the decimal point, 1 for the decimal point and two before the decimal point making %19.16f

Add %n so you get a new line at the end of the output.

You realise that a

I have added code tags to your post; you should always use them. See how much better it looks Never import java.lang.Math.

You have misunderstood the figures in the format String. 1. does not mean one digit then the decimal point. It means take 1 space for the whole output. You want 16 for the places after the decimal point, 1 for the decimal point and two before the decimal point making %19.16f

Add %n so you get a new line at the end of the output.

You realise that a

`double`is only precise to about 15.9 decimal places, so you are guaranteed inaccurate output if you try to print 18 digits, as in %19.16f?
posted 3 years ago

I had a little problem with understanding why you suggested that.

I was thinking "What is wrong with

Only after a little time I realized what you meant by this. You should not import anything from

Campbell Ritchie wrote:Never import java.lang.Math.

I had a little problem with understanding why you suggested that.

I was thinking "What is wrong with

`Math`that it should not be imported???"

Only after a little time I realized what you meant by this. You should not import anything from

`java.lang`package because there is an implicit

`import java.lang.*`

Nikhil Nair

Greenhorn

Posts: 2

posted 3 years ago

Thank you for the reply.

I am working on this assignment.I wrote this program keeping in mind the output the console showed me.

Browser enterd 12 as input square root result: ((Desired output) 3.4641016151377544 , (My Output) 3.4641016151377544 ) , quadratic root result ( (Desired output)

Hope you all understood what i am trying to say

Thanks

I am working on this assignment.I wrote this program keeping in mind the output the console showed me.

Browser enterd 12 as input square root result: ((Desired output) 3.4641016151377544 , (My Output) 3.4641016151377544 ) , quadratic root result ( (Desired output)

**1.8612097182041991**, (My Output)**1.861209718204199**) , Square root result was ok but quadratic root result was 1 short of required result so i increased the precision to 16 digit but when i compiled it again this time browsers input was 235.3 square root result: ((Desired output) 15.339491516996253 , (My Output) 15.339491516996253 ) , quadratic root result ( (Desired output)**3.916566291663688**, (My Output)**3.9165662916636880**) Again my result had one digit extra like 16 but the desired output had 15 digits. My question is is their any way to control the output according to input like how can i know what user input will give them 15th precision or 16th.Hope you all understood what i am trying to say

Thanks